4. Quick sort:
Do u have doubt why these p and q variables..?
The p will be looking for the elements that are greater
than pivot and q for lesser elements. So when p finds a greater
element, it comes out of the while loop and q finds a lesser
element it comes out of the loop and these elements are
interchanged their positions i.e. Swapping .
http://comsciguide.blogspot.com/2014/10/quick-sort-quick-sort-
is-most-general.html
5. Quick sort:
P Q
29 | 13 | 47 | 25 | 11 | 3 | 9 | 37
P Q
29 | 13 | 47 | 25 | 11 | 3 | 9 | 37
P Q
29 | 13 | 9 | 25 | 11 | 3 | 47 | 37
Before while loop starts :
After while loop starts :
Before interchanging :
After interchanging :
And this process carry on untill both will meet or
crossover each other. After the pivot element is
interchanged with q
6. Quick sort:
P Q
29 | 13 | 9 | 25 | 11 | 3 | 47 | 37
The next greater element is 47 and lesser element is 3. So if P
comes to 47 and q comes to 3 ,by this time the two variables
crossover each other. So there will be no swapping hereafter
between P and Q. Atlast the q and pivot will be interchanged.
29 | 13 | 9 | 25 | 11 | 3 | 47 | 37
P
Q
3 | 13 | 9 | 25 | 11 | 29 | 47 | 37
P
Q
Pivot
Pivot
Left array 3 | 13 | 9 | 25 | 11 29 47 | 37 Right array
7. Quick sort: 3 | 13 | 9 | 25 | 11 | 29 | 47 | 37
Using Recursion
again the left and
right elements are
sorted in the same
manner.
3 | 13 | 9 | 25 | 11 29
47 | 37
13 | 9 | 25 | 11 37 | 47
13 | 9 | 11 | 25
11 | 9 | 13 | 25
11 | 9 25
9 | 11
29
29
3 | 9 |11 | 13 | 25 | 29 | 37 | 47
3
3
3
3 13
37 | 47
37 | 47
37 | 47
3 13 25
29
29
29 37 | 47
Finally The sorted array is A[ ] =