0% found this document useful (0 votes)
8 views

sorting

Quick sort data structure

Uploaded by

Sethi Lilu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

sorting

Quick sort data structure

Uploaded by

Sethi Lilu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

peular soting algorthn that follows the

dwite-and Conguer prncjale I woks by selecting


"pwo element from the amay and partitioning the ofher
element onto two sub arrayo accordng to whether they
are less than or geater than the paet The sub- arrays
are then sortd ecursiely

# # Shps f the Qutksot Agorthmu


Puo* Select ar element ron the
arrey to be the pot Comman stratgts iclude picking
the first element the last element a ranton element
r the median

2. *Pattioning** Rearange the arey so that all


elements ess than the pwot cone beforr t andt all
elements greater than the pwot come affer t The pwot

3 **Recusian **, Recusiely agaly the aboe steps to


the sub arrays formed by splting arund the pávot
##* Eample:

1 **Chose Pwo*; Let's choase the last element

2. **Pattioning*
Znitielige ponttrs to move thrugh the array
Compare each element with the past
-

When yoe find an elemert less than the


pwot 0, swap t fowards the front
After partioning the aray might loak lke tho:
pastion

The left sub arrey &empty end the right


Repeat steps and 2 an the right sub aray
wntl all parts are sofd

# # Firal Sotd Array

Afer ncrsiely soting the sub arays the fnal sottd


aray wdl be: Cl 4 2 3 6 8 01.
##* 7Time Complenty
Best and Average Case: On log n)
Worst Case: O ) (occus when the smallest or
Carest eement o always chosen as the pävo
# F Conclusior

fcant or Carge data sets ant s


conanly wsed in ractioe due to to aerage
performance and cache fcency Howees careful
selectin f tie paot can signiftcantly ec s
perfomance
Consider the folowing unsorted list of elements...

Define pivot, left &right. Set pivot =0, left 1& right = 7. Here 7 indicates 'size-1'.
left right

List 538 146 2 7


pivot

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

You might also like