0% found this document useful (0 votes)
12 views16 pages

Data Structure Using C Quick Sort: UNIT 2 - Searching and Sorting

Uploaded by

kotiansaheb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Data Structure Using C Quick Sort: UNIT 2 - Searching and Sorting

Uploaded by

kotiansaheb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Data Structure Using C

UNIT 2 – Searching and Sorting


Quick Sort

Prepared By
Harshala Date
Sr. Lecturer
Department Of Computer Engineering
Unit Outcomes

a. Write an algorithm to sort data using quick sort method

b. Explain the working of quick sort method step-by-step with an example


Learning Outcomes

 Understand Sorting definition

 Understand Concept of Quick Sort

 Understand working of Quick Sort

 Understand example of Quick Sort

 Understand time complexity of Quick Sort

 Advantages Of Quick sort

 Disadvantages Of Quick sort

 Comparison of Sorting Techniques based on Time Complexity


Sorting

Sorting is process of arranging the elements of an array in some logical


order (ascending or descending).

Internal Sorting Techniques

1. Bubble Sort (Exchange Sort)


2. Selection Sort (Exchange Sort)
3. Insertion Sort (Insert and shift)
4. Radix Sort
5. Quick Sort (Partition & Exchange)
Concept of Quick Sort(Partition-Exchange)

 Quick sort is a highly efficient sorting algorithm

 It is based on partitioning of array of data into smaller arrays.

 A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the
partition is made and another array holds values greater than the pivot
value.

 For example

1 4 2 8 44 67 20 34
Working of Quick Sort(Partition-Exchange)

Step 1 − Initialize 0th element as pivot element, variable a to 1st element


and index variable b to (n-1)th element .

Step 2 − job of index variable a is to find element greater than or equal to


pivot element & job of index variable b is to find element
smaller than pivot element

Step 3 −After adjusting a & b , if a &b are not crossed or at same


position then swap element at a & element at b.

Step 4 − Repeat step 2 &3 until a & b are crossed or at same position.

Step 5 − if a &b are crossed or at same position then swap element at b


& pivot element. 1 * = 0th a=1st b= (n-1)th
2 a >= * b<*
if a=b OR cross
swap b & *
3
else
swap a & b
Working of Quick Sort
Step 6 − After swapping pivot ,we get 2 sub arrays .first sub-array
contains elements less than pivot and second sub-array
contains elements greater or equal to pivot.

Step 7 − Repeat steps 1 to 6 for each sub-arrays until each sub-arrays


have only 1 element.

Step 8 − End

1 * = 0th a=1st b= (n-1)th


2 a >= * b<*
if a=b OR cross
swap b & *
3
else
swap a & b
Quick Sort
1 * = 0th a=1st b= (n-1)th
Example 1- 8 , 34 , 2 , 67 , 44 , 1 , 20 , 4 2 a >= * b<*
if a=b OR cross
Solution :- swap b & *
3
else
swap a & b
0 1 2 3 4 5 6 7
Pass 1-
8 34 2 67 44 1 20 4

8 4 2 67 44 1 20 34
Quick Sort

8 4 2 1 44 67 20 34 1 * = 0th a=1st b= (n-1)th


2 a >= * b<*
if a=b OR cross
swap b & *
3
else
swap a & b

1 4 2 8 44 67 20 34
Quick Sort
1 * = 0th a=1st b= (n-1)th
Pass 2 – 2 a >= * b<*
if a=b OR cross
swap b & *
1 4 2 8 44 67 20 34 3
else
swap a & b

1 4 2 8 44 34 20 67

1 4 2 8 20 34 44 67
Quick Sort
1 * = 0th a=1st b= (n-1)th
Pass 3 -
2 a >= * b<*
if a=b OR cross
1 4 2 8 20 34 44 67
swap b & *
3
else
swap a & b

1 2 4 8 20 34 44 67

Hence sorted list is ,

1 2 4 8 20 34 44 67
Time Complexity

 Best case time complexity :- O(n log n)

 Average case time complexity :- O(n log n)

 Worst case time complexity :- O(n2)


Advantages
 Its average-case time complexity to sort an array of n elements is

O(n log n).

 On the average it runs very fast, even faster than Merge Sort.

 It requires no additional memory


Disadvantages
 Its worst-case running time, O(n2) to sort an array of n elements.

 It is not stable.
Sorting Techniques Time Complexity
.

Sorting Technique Best Average Worst

Bubble Sort O(n) O(n2) O(n2)

Selection Sort O(n) O(n2) O(n2)

Insertion Sort O(n) O(n) O(n2)


Radix Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
THANK YOU

You might also like