Quick Sort Algorithm
Quick Sort Algorithm
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than
specified value say pivot based on which the partition is made and another array holds values
greater than pivot value.
The quick sort partitions an array and then calls itself recursively twice to sort the resulting two
subarray. This algorithm is quite efficient for large sized data sets as its average and worst case
complexity are of Onlogn where n are no. of items.
Partition in Quicksort
The below given animated representation explains how to find pivot value in the array.
The pivot value divides the list in to two parts. And recursively we find pivot for each sub-lists until
all lists contains only one element.
while True do
while A[++leftPointer] < pivot do
//do-nothing
end while
end while
swap leftPointer,right
return leftPointer
end function
QuickSort Algorithm
Using pivot algorithm recursively we end-up with smaller possible partitions. Each partition then
processed for quick sort. We define recursive algorithm for quicksort as below −
QuickSort Pseudocode
To get more into it, let see the pseudocode for quick sort algorithm −
if right-left <= 0
return
else
pivot = A[right]
partition = partitionFunc(left, right, pivot)
quickSort(left,partition-1)
quickSort(partition+1,right)
end if
end procedure