sorting
sorting
2. **Pattioning*
Znitielige ponttrs to move thrugh the array
Compare each element with the past
-
Define pivot, left &right. Set pivot =0, left 1& right = 7. Here 7 indicates 'size-1'.
left right
Compare List[lef with List(pivo. If List[lef is greater than Lstipvot] then stop let otherwise move left
to the next.
Compare List[right] with List(pivot]. IF List[right is snaller than List{pivot) then stop right otherwise move
right to the previous.
Repeat the same until left>=right.
f both left &right are stopec but left <rightthen swap Listlleft] with Listright) and countinue the process.
If left>=right then swap Listtpivotl with List(right.
lefs right
List
S|3814|6|2|7
pivot
Compare List(leftj<List(pivot] as it is true increment left by one and repeat the same, left will stop at 8.
Compere ist[right]> List{pivot] as it is true decrement right by one and repeat the same, right will stop at 2.
left right
List538146 27
pivot
Here left &right both are stopod and left is not greater than right so we nocd to swap List(left) and List[right)
left right
List 53 2 1 4687
pivot
Compare List{lef< List(plvot) as it is true increment left by one and repeat the same, left will stop at 6.
Compare Listright >listipivot) as it is true decrement right by one and repeat the same, right will stop at 4.
right left
List
SB2146|B7
plvot
Here left &right both are stoped and left is greater than right so we need to swap List(pivot] and List(righe]
List 43 2 1 5687
Here we can observe that all the numbers to the left side of 5 anre smaller and right side are greater. That
means S is placed in its correct position.
Repeat the same process on the left sublist and right sublist to the number 5.
Ieft ight left right
List42|156s7
pivot pivot
In the left sublist as there are no smaller number than the pivot left will keep on moving to the nent and stops
at last number. As the Listiright| is smaller, right stops at same position. Now left and right both are equal so
we swap pivot with right.
left right
List1|32456|87 pivet
In the rlght sublist left Is grester than the plvOt, left wiIl stop at same position.
As the List(right) is greater than Listpvot] ight moves towords left and stops at pivot number postion.
Now left > right so we swap plvot with right. (6 Is swap by Itself).
left right
List 1|3|245s|7
plvot
Repeat the same recursively on both left and right sublists until all the numbers are sorted.
The final sorted list will be as follows.
List 12B4578