Algorithm:
- If First < Last, then
- Partition the elements in the sub-array First to Last so that the pivot element is in place.
- Apply quick sort to the first sub-array First to pivindex-1.
- Apply quick sort to the second sub-array pivindex+1 to Last.
The two Stopping cases:
- If First=Last,Only one value in the array, So sorted.
- If First > Last, no values in the array, So sorted.
- Define the Pivot value (mostly choose first element in the array).
- Initialize Up to First element and Down to last element.
- Increment Up until Up > pivot.
- Decrement Down until Down <= pivot.
- If Up < Down exchange their values.
- Repeat 3,4,5 until Up meets or Passes Down.
- Exchange array[First] and array[Down].
- Define pivindex as Down.
Consider the array elements as 4,2,3,5,1.
Here First(as well as Up) and Last(as well as Down) elements are 4 and 1 respectively. Let the Pivot element be 3.
No comments:
Post a Comment