Quick Sort: As The Name Implies, It Is Quick, and It Is The Algorithm Generally Preferred For Sorting
Quick Sort: As The Name Implies, It Is Quick, and It Is The Algorithm Generally Preferred For Sorting
As the name implies, it is quick, and it is the algorithm generally preferred for sorting.
Quick Sort
Basic Ideas
(Another divide-and-conquer algorithm) Pick an element, say P (the pivot) Re-arrange the elements into 3 sub-blocks,
those less than or equal to () P (the left-block S1) 2. P (the only element in the middle-block) 3. those greater than or equal to () P (the rightblock S2)
1.
Repeat the process recursively for the left- and right- sub-blocks. Return {quicksort(S1), P, quicksort(S2)}. (That is the results of quicksort(S1), followed by
P, followed by the results of quicksort(S2))
Quick Sort 2
Basic Ideas
Pick a Pivot value, P Create 2 new sets without P Items smaller than or equal to P Items greater than or equal to P
0 31 13 43 26 57
quicksort(S1)
65
75 92 81
quciksort(S2)
0 13 26 31 43 57
65
75 81 92
0 13 26 31 43 57 65 75 81 92
Quick Sort
Basic Ideas
S is a set of numbers
S1 = {x S {P} | x P}
S2 = { x S {P} | P x}
Quick Sort
Basic Ideas
Note: The main idea is to find the right position for the pivot element P. After each pass, the pivot element, P, should be in place. Eventually, the elements are sorted since each pass puts at least one element (i.e., P) into its final position. Issues: How to choose the pivot P ? How to partition the block into sub-blocks?
Quick Sort 5
Implementation
Algorithm I:
int partition(int A[ ], int left, int right); // sort A[left..right] void quicksort(int A[ ], int left, int right) { int q ; if (right > left ) { q = partition(A, left, right); // after partition // A[left..q-1] A[q] A[q+1..right] quicksort(A, left, q-1); quicksort(A, q+1, right); } }
Quick Sort 6
Implementation
// select A[left] be the pivot element) int partition(int A[], int left, int right); { P = A[left]; i = left; j = right + 1; for(;;) //infinite for-loop, break to exit { while (A[++i] < P) if (i >= right) break; // Now, A[i] P while (A[--j] > P) if (j <= left) break; // Now, A[j] P if (i >= j ) break; // break the for-loop else swap(A[i], A[j]); } if (j == left) return j ; swap(A[left], A[j]); return j; }
Quick Sort 7
Example
Input:
P: 65 Pass 1: (i) (ii) (iii) (iv) (v) 65 70 75 i 65 70 75 i 65 45 75 i 65 45 50 80 85 60 55 50 45 80 85 60 55 50 45 j swap (A[i], A[j]) 80 85 60 55 50 70 j swap (A[i], A[j]) 80 85 60 55 75 70 i j swap (A[i], A[j]) 65 45 50 55 85 60 80 75 70 i j swap (A[i], A[j]) 65 45 50 55 60 85 80 75 70 if (i>=j) break j i 60 45 50 55 65 85 80 75 70 swap (A[left], A[j])
Items greater than or equal to 65 Quick Sort 8
Example
Result of Pass 1: 3 sub-blocks: 60 45 50 55 65 85 80 75 70 Pass 2a (left sub-block): 60 45 50 55 (P = 60) i j 60 45 50 55 j i if (i>=j) break 55 45 50 60 swap (A[left], A[j]) Pass 2b (right sub-block): 85 80 75 70 (P = 85) i j 85 80 75 70 j i if (i>=j) break 70 80 75 85 swap (A[left], A[j])
Quick Sort
Quick Sort
10
Quick Sort
13
Pick three elements, and find the median x of these elements. Use that median as the pivot. Randomly pick a element as a pivot.
Random element
Quick Sort
14
Quick Sort
15
Quick Sort
17
Quick Sort
18
Quick Sort
19
Quick Sort
20
Though we claim it is a fast algorithm, the worst-case running time is O(n2) (see if you can prove it). But the average-case running time is only O(n). (Again, we will see the analysis in COMP 271). There is an algorithm that runs in O(n) in the worst case, again, we will talk about that in COMP 271.
Quick Sort
21