KEMBAR78
Quick sort | PPTX
Quick Sort
By Amar Kakde
Quick Sort
• Quick sort is also called as partition exchange sort.
• The main principle of this method is divide and rule.
• The quick sort algorithm works by partitioning the
array to be sorted, and each partition is internally
sorted using recursion.
Quick Sort
• While partitioning the array first element of the array is
considered as key value/pivot value.
• The aim is to place this pivot value at its correct
position in the array such that
One partition contains values smaller than pivot.
Another partition contains values greater than the pivot
• Array will be partitioned into two pieces at the final position
of pivot value.
Quick Sort Algorithm
1. Choose the pivot element pivot = a[low]
2. Put all elements less than pivot on the left ; put all elements
Larger than pivot on the right; put pivot at the middle
(partition).
3. Quicksort the array on the left of pivot recursively.
4. Quicksort the array on the right of pivot recursively.
Quick Sort
 Two index variables P & Q are used to indicate lower and
upper bound of current array partition.
 Keep incrementing P until you find the value which is greater
than pivot.
 Keep decrementing Q until you find the value which is smaller
than pivot.
 At this stage if Q is greater than P, interchange the values at P
& Q indexes.
 If Q is smaller or Equal to P that means this is the final
position of Pivot element, and array can be partitioned into
two group at this index.
Quick Sort
10 5 27 15 4 6 8 25 88
PIVOT
ELEMENT
0 1 2 3 4 5 6 7 8
P q
10 5 27 15 4 6 8 25 88
P q
10 5 27 15 4 6 8 25 88
P q
10 5 8 15 4 6 27 25 88
qP
Quick Sort
10 5 8 15 4 6 27 25 88
0 1 2 3 4 5 6 7 8
P q
10 5 8 15 4 6 27 25 88
P q
10 5 8 6 4 15 27 25 88
P q
10 5 8 6 4 15 27 25 88
q P
Quick Sort
0 1 2 3 4 5 6 7 8
4 5 8 6 10 15 27 25 88
q P
Quick Sort
Algorithm Time Notes
selection-sort O(n2)
in-place
slow (good for small inputs)
insertion-sort O(n2)
in-place
slow (good for small inputs)
quick-sort
O(n log n)
expected
in-place, randomized
fastest (good for large inputs)
heap-sort O(n log n)
in-place
fast (good for large inputs)
merge-sort O(n log n)
sequential data access
fast (good for huge inputs)

Quick sort

  • 1.
  • 2.
    Quick Sort • Quicksort is also called as partition exchange sort. • The main principle of this method is divide and rule. • The quick sort algorithm works by partitioning the array to be sorted, and each partition is internally sorted using recursion.
  • 3.
    Quick Sort • Whilepartitioning the array first element of the array is considered as key value/pivot value. • The aim is to place this pivot value at its correct position in the array such that One partition contains values smaller than pivot. Another partition contains values greater than the pivot • Array will be partitioned into two pieces at the final position of pivot value.
  • 4.
    Quick Sort Algorithm 1.Choose the pivot element pivot = a[low] 2. Put all elements less than pivot on the left ; put all elements Larger than pivot on the right; put pivot at the middle (partition). 3. Quicksort the array on the left of pivot recursively. 4. Quicksort the array on the right of pivot recursively.
  • 5.
    Quick Sort  Twoindex variables P & Q are used to indicate lower and upper bound of current array partition.  Keep incrementing P until you find the value which is greater than pivot.  Keep decrementing Q until you find the value which is smaller than pivot.  At this stage if Q is greater than P, interchange the values at P & Q indexes.  If Q is smaller or Equal to P that means this is the final position of Pivot element, and array can be partitioned into two group at this index.
  • 6.
    Quick Sort 10 527 15 4 6 8 25 88 PIVOT ELEMENT 0 1 2 3 4 5 6 7 8 P q 10 5 27 15 4 6 8 25 88 P q 10 5 27 15 4 6 8 25 88 P q 10 5 8 15 4 6 27 25 88 qP
  • 7.
    Quick Sort 10 58 15 4 6 27 25 88 0 1 2 3 4 5 6 7 8 P q 10 5 8 15 4 6 27 25 88 P q 10 5 8 6 4 15 27 25 88 P q 10 5 8 6 4 15 27 25 88 q P
  • 8.
    Quick Sort 0 12 3 4 5 6 7 8 4 5 8 6 10 15 27 25 88 q P
  • 9.
    Quick Sort Algorithm TimeNotes selection-sort O(n2) in-place slow (good for small inputs) insertion-sort O(n2) in-place slow (good for small inputs) quick-sort O(n log n) expected in-place, randomized fastest (good for large inputs) heap-sort O(n log n) in-place fast (good for large inputs) merge-sort O(n log n) sequential data access fast (good for huge inputs)