Reputation: 748
I was studying Quick sort, I found the algorithm explained here
best to my understanding, but I have a question at one of the step.
Could someone explained me properly what would be the steps till the pivot 57 is kept to its right placed, if the number 76 in this point as shown in the image was 7 ?
I think it would be more helpful if the reader first see the steps explained in the slides as I found out there are many other different approach for explaining quick sort algorithm.
Editted: I guessed the final sequenced would be like
24 49 16 38 55 21 36 9 *7 *57 81 85 63 79 74 85 97 61 77 70 *68. (as mentioned by nullpointer)
Did the flow stopped as the blue finds 68 as the greatest element on the right side and skipped checking lesser element as the index of blue crossed/ met the red index?
Upvotes: 4
Views: 436
Reputation: 28931
A variation of quicksort:
void swap(int *i, int *j)
{
int t = *i;
*i = *j;
*j = t;
}
void QuickSort(int a[], int lo, int hi) {
int i = lo, j = (lo + hi)/2, k = hi;
int pivot;
if (a[k] < a[i]) // median of 3
swap(a+k, a+i);
if (a[j] < a[i])
swap(a+j, a+i);
if (a[k] < a[j])
swap(a+k, a+j);
pivot = a[j];
showa(lo, hi);
while (i <= k) { // partition
while (a[i] < pivot)
i++;
while (a[k] > pivot)
k--;
if (i <= k) {
swap(a+i, a+k);
i++;
k--;
showa(lo, hi);
}
}
if (lo < k) // recurse
QuickSort(a, lo, k);
if (i < hi)
QuickSort(a, i, hi);
}
The output, with '*' after a swapped number:
57 70 97 38 63 21 85 68 76 9 81 36 55 79 74 85 16 61 77 49 24
24*70 97 38 63 21 85 68 76 9 57*36 55 79 74 85 16 61 77 49 81*
24 49*97 38 63 21 85 68 76 9 57 36 55 79 74 85 16 61 77 70*81
24 49 16*38 63 21 85 68 76 9 57 36 55 79 74 85 97*61 77 70 81
24 49 16 38 55*21 85 68 76 9 57 36 63*79 74 85 97 61 77 70 81
24 49 16 38 55 21 36*68 76 9 57 85*63 79 74 85 97 61 77 70 81
24 49 16 38 55 21 36 57*76 9 68*85 63 79 74 85 97 61 77 70 81
24 49 16 38 55 21 36 57 9*76*68 85 63 79 74 85 97 61 77 70 81
9*49 16 38 24*21 36 57 55*
9 21*16 38 24 49*36 57 55
9 21 16 24*38*49 36 57 55
9 21 16 24
9 16*21*24
9 16
9 16
21 24
21 24
36*49 38*57 55
36 38*49*57 55
36 38
36 38
49 55*57*
49 55 57
74*68 85 63 79 76*85 97 61 77 70 81
74 68 70*63 79 76 85 97 61 77 85*81
74 68 70 63 61*76 85 97 79*77 85 81
74 68 70 63 61 76 85 97 79 77 85 81
61*68 70 63 74*
61 68 63*70*74
61 63*68*
61 63 68
70 74
70 74
79*97 81*77 85 85*
79 77*81 97*85 85
79 77 81 97 85 85
77*79*
77 79
85*85 97*
85 85 97
85 97
85 97
9 16 21 24 36 38 49 55 57 61 63 68 70 74 76 77 79 81 85 85 97
Upvotes: 2
Reputation: 32036
Contd..[* blue pointer ; ** red pointer ; *** vacant] Pivot =57
=> 24 49 16 38 55 21 36 *68 7 **9 81 85 63 79 74 85 97 61 77 70 ***
=> 24 49 16 38 55 21 36 *9 7 **68 81 85 63 79 74 85 97 61 77 70 ***
I expect things until this would be clear as they are as it is. 9 and 68 are swapped. Now the next number smaller than 57 from right end is 7(so **) and number larger than it form left is 68(so *).
=> 24 49 16 38 55 21 36 9 **7 *68 81 85 63 79 74 85 97 61 77 70 ***
But since the indexes would not satisfy the conditions further, hence the number with red pointer 68 would be moved to the vacant space and 57 to its position in the middle. Hence the sequence should be :
=> 24 49 16 38 55 21 36 9 **7 *57 81 85 63 79 74 85 97 61 77 70 ***68
Upvotes: 3