Quick-Sort Algorithm
Quick-Sort Algorithm
Algorithms
Course Code: CSC2211 Course Title: Algorithms
1. Sorting Algorithms
Quick Sort
Quick Sort
• Quick sort is based on the divide-and-conquer approach.
Quicksort
A divide & conquer algorithm
Efficient sorting algorithm
Discovered by C.A.R. Hoare
Basic idea:
Pick an element (called pivot)
Partition the array in two subsequences: those smaller than
the pivot and those larger or equal: Divide
Sort each subsequence recursively: Conquer
6
Quicksort
Place the pivot in its appropriate position
< pivot1 pivot1 > pivot1 pivot < pivot2 pivot2 > pivot2
8
Quicksort: Code
void quicksort( int *a, int low, int high )
{
int pivot;
/* Termination condition! */
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
int partition( int *a, int low, int high )
{
int left, right, pivot_item;
pivot_item = a[low];
pivot = left = low;
right = high;
while ( left < right )
{
/* Move left while item < pivot */
while( a[left] <= pivot_item )
left++;
/* Move right while item > pivot */
while( a[right] >= pivot_item )
right--;
if ( left < right )
SWAP(a[left],a[right]);
}
/* Right is final position for the pivot */
a[low] = a[right];
a[right] = pivot_item;
return right;
}
10
23 12 15 38 42 18 36 29 27
low high
11
left right
23 12 15 38 42 18 36 29 27
right--;
left right
23 12 15 38 42 18 36 29 27
left
23 12 15 38 42 18 36 29 27
left
23 12 15 38 42 18 36 29 27
left
23 12 15 38 42 18 36 29 27
left
23 12 15 38 42 18 36 29 27
left right
23 12 15 38 42 18 36 29 27
left right
23 12 15 38 42 18 36 29 27
left right
23 12 15 38 42 18 36 29 27
left right
23 12 15 38 42 18 36 29 27
left right
23 12 15 18 42 38 36 29 27
pivot: 23
low high
23
left right
23 12 15 18 42 38 36 29 27
pivot: 23
low high
24
left right
23 12 15 18 42 38 36 29 27
low high
pivot: 23
25
left
right
23 12 15 18 42 38 36 29 27
low high
pivot: 23
26
left
right
23 12 15 18 42 38 36 29 27
low high
pivot: 23
27
left
right
23 12 15 18 42 38 36 29 27
low high
pivot: 23
28
right left
23 12 15 18 42 38 36 29 27
a[low] = a[right];
a[right] = pivot_item; Finally, swap the
pivot
and right
29
right
pivot: 23
18 12 15 23 42 38 36 29 27
low high
Return the
return right; position
of the pivot
30
Recursively Recursively
sort left half sort right half
Conquer
Quicksort
23 12 15 38 42 18 36 29 27
18 12 15 42 38 36 29 27
23
38 36 29 27 42
12 15 18
36 29 27 38
12 15
29 27 36
27 29
12 15 18 23 27 29 36 38 42
Quick Sort Analysis
Substitution Method
33
Quicksort: Analysis
Quicksort: Analysis
Worst case:
Partitioning n elements results in two subsequences of
lengths 1 and n-1
What happens if we use quicksort on
data that’s already sorted (or nearly sorted)?
Now Take a look
41
Quicksort: Analysis
Sorted Data
pivot
? 1 2 3 4 5 6 7 8 9
pivot
2 3 4 5 6 7 8 9
> pivot
Worst Case T(n), to sort
n elements
void quicksort( void *a, int low, int high ) c1
{
int pivot;
/* Termination condition! */
if ( high > low )
{
0, 1, 3, 6
= 0, 1, (1+2), (1+2+3)
Kth term
1+2+3+....+(k-1) =
45
46
pivot
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Median is 5
Perfect division of sorted data every time!
O(n log n) time
Key requirement
• Pivot choice must take O(1) time
Ashiq, MIST
50
1 4 9 6 2 5 7 8 3
Here median-of-3 chooses 2
One partition of size 1 and 1 2 4 9 6 5 7 8 3
One partition of size 7
Next it chooses 4
One partition of size 1 and
One partition of size 5
51
Key points
Quicksort
O(n log n) but ….
Can be O(n2)
https://www.google.com/search?q=bubble+sort+step+by+step&sxsrf=AL
eKk01uxzgfT3Oy6k1Q3WxVnSpiIN8_4g:1587999728942&tbm=isch&sourc
e=iu&ictx=1&fir=vRwFsGwVfJ6pJM%253A%252CSzhhze6MPQr4cM%252C
_&vet=1&usg=AI4_-kSrEEXqwRL-PkHhVUtn7jNfF9dB6g&sa=X&ved=2ahU
KEwje0Pz974jpAhXRAnIKHWhMD2UQ_h0wAXoECAcQBg#imgrc=EN4Sdu7
veOWVoM&imgdii=eOqvCu85p9-eBM
https://www.interviewcake.com/concept/java/counting-sort
https://www.geeksforgeeks.org/counting-sort/
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tut
orial/