Monday, 18 July 2011

Quick sort Example.

Example:
1) Consider the example with array elements 44,75,23,43,55,12,64,77.
2) The First(UP) and Last(Down) element are 44 and 33 resp.
 3) Let the pivot element be 44.
4) Increment Up until Up > Pivot.

 5) Decrement Down Until Down <= Pivot.


 6) Now, Both the Conditions satisfies. So, Exchange values of Up and Down.
7) Again Increment Up until Up > Pivot.
8) Similarly decrement Down until Down <= Pivot.
9) Now swap the values of Up and Down Values.
10) Again increment the Up pointer until Up > pivot.
11) Similarly decrement the Down until Down < = Pivot.
12) Up and Down passed each other, so exchange the pivot value and the value in the Down.
13) Note that all the values below the pivindex are <= pivot and the values above the pivindex are > pivot.
14) This gives us two new sub-arrays.
15) Repeat the process for the rest of the sub-arrays.

No comments:

Post a Comment