QUICK SORT
Quick sort Uses divide-and-conquer approach Divide:  If  S  has at least two elements select a specific element  x  from  S , called the pivot. Partition  S  into L storing elements in  S  less than  x E   storing elements in  S  equal to  x G storing elements in  S  greater than or equal to  x Recur:  recursively sort sequences L and G Conquer:  put elements in L, E and G back into  S  in order
GIVEN  SERIES 5 3 8 9 1 7 0 2 6 4
SELECT THE PIVOT ELEMENT P 5 3 8 9 1 7 0 2 6 4
P  L  R ITERATION-1 5 3 8 9 1 7 0 2 6 4
SWAPE THE ELEMENTS 5 3 4 9 1 7 0 2 6 8
SWAP THE ELEMENTS P  L  R 5 3 4 9 1 7 0 2 6 8 5 3 4 2 1 7 0 9 6 8
P  L  R SWAP THE ELEMENTS P  R  L 5 3 4 2 1 7 0 9 6 8 5 3 4 2 1 0 7 9 6 8
P > R > L 0 3 4 2 1 5 7 9 6 8 0 3 4 2 1 5 7 9 6 8
P  L  R  P  L  R ITERATION-2 SWAP THE ELEMENTS 0 1 4 2 3 5 7 6 9 8 0 3 4 2 1 5 7 9 6 8
P  L  R  P  L  R SWAP THE ELEMENTS 0 1 4 2 3 5 6 7 9 8 0 1 2 4 3 5 6 7 9 8
SWAP THE ELEMENTS 5 6 7 9 8 0 1 2 4 3 5 6 7 8 9 0 1 2 3 4
0 1 2 3 4 5 6 7 8 9

Quick Sort

  • 1.
  • 2.
    Quick sort Usesdivide-and-conquer approach Divide: If S has at least two elements select a specific element x from S , called the pivot. Partition S into L storing elements in S less than x E storing elements in S equal to x G storing elements in S greater than or equal to x Recur: recursively sort sequences L and G Conquer: put elements in L, E and G back into S in order
  • 3.
    GIVEN SERIES5 3 8 9 1 7 0 2 6 4
  • 4.
    SELECT THE PIVOTELEMENT P 5 3 8 9 1 7 0 2 6 4
  • 5.
    P L R ITERATION-1 5 3 8 9 1 7 0 2 6 4
  • 6.
    SWAPE THE ELEMENTS5 3 4 9 1 7 0 2 6 8
  • 7.
    SWAP THE ELEMENTSP L R 5 3 4 9 1 7 0 2 6 8 5 3 4 2 1 7 0 9 6 8
  • 8.
    P L R SWAP THE ELEMENTS P R L 5 3 4 2 1 7 0 9 6 8 5 3 4 2 1 0 7 9 6 8
  • 9.
    P > R> L 0 3 4 2 1 5 7 9 6 8 0 3 4 2 1 5 7 9 6 8
  • 10.
    P L R P L R ITERATION-2 SWAP THE ELEMENTS 0 1 4 2 3 5 7 6 9 8 0 3 4 2 1 5 7 9 6 8
  • 11.
    P L R P L R SWAP THE ELEMENTS 0 1 4 2 3 5 6 7 9 8 0 1 2 4 3 5 6 7 9 8
  • 12.
    SWAP THE ELEMENTS5 6 7 9 8 0 1 2 4 3 5 6 7 8 9 0 1 2 3 4
  • 13.
    0 1 23 4 5 6 7 8 9