Quicksort is a divide and conquer algorithm that works by partitioning an array around a pivot value and recursively sorting the sub-partitions. It first chooses a pivot element and partitions the array by placing all elements less than the pivot before it and all elements greater than it after it. It then recursively quicksorts the two partitions. This continues until the individual partitions only contain single elements at which point they are sorted. Quicksort has average case performance of O(n log n) time making it very efficient for large data sets.
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.
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)