Quicksort: Pseudo Code For Recursive Quicksort Function
Quicksort: Pseudo Code For Recursive Quicksort Function
Quicksort: Pseudo Code For Recursive Quicksort Function
geeksforgeeks.org/quick-sort
January 7, 2014
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as
pivot and partitions the given array around the picked pivot. There are many different
versions of quickSort that pick pivot in different ways.
The key process in quickSort is partition(). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all
smaller elements (smaller than x) before x, and put all greater elements (greater than x)
after x. All this should be done in linear time.
1/11
Partition Algorithm
There can be many ways to do partition, following pseudo code adopts the method
given in CLRS book. The logic is simple, we start from the leftmost element and keep
track of index of smaller (or equal to) elements as i. While traversing, if we find a
smaller element, we swap current element with arr[i]. Otherwise we ignore current
element.
2/11
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
partition (arr[], low, high)
{
// pivot (Element to be placed at right position)
pivot = arr[high];
Illustration of partition() :
3/11
arr[] = {10, 80, 30, 90, 40, 50, 70}
Indexes: 0 1 2 3 4 5 6
Implementation:
Following are the implementations of QuickSort:
C++
C
Java
Python
C#
4/11
filter_none
edit
play_arrow
brightness_4
def partition(arr,low,high):
i = ( low - 1 )
pivot = arr[high]
i = i + 1
arr[i],arr[j] = arr[j],arr[i]
def quickSort(arr,low,high):
if low < high:
pi = partition(arr,low,high)
quickSort(arr, low, pi - 1 )
quickSort(arr, pi + 1 , high)
arr = [ 10 , 7 , 8 , 9 , 1 , 5 ]
n = len (arr)
quickSort(arr, 0 ,n - 1 )
print ( "Sorted array is:" )
for i in range (n):
print ( "%d" % arr[i]),
Output:
Sorted array:
1 5 7 8 9 10
Analysis of QuickSort
Time taken by QuickSort in general can be written as following.
The first two terms are for two recursive calls, the last term is for the partition process. k
is the number of elements which are smaller than pivot.
The time taken by QuickSort depends upon the input array and partition strategy.
5/11
Following are three cases.
Worst Case: The worst case occurs when the partition process always picks greatest or
smallest element as pivot. If we consider above partition strategy where last element is
always picked as pivot, the worst case would occur when the array is already sorted in
increasing or decreasing order. Following is recurrence for worst case.
Best Case: The best case occurs when the partition process always picks the middle
element as pivot. Following is recurrence for best case.
The solution of above recurrence is (nLogn). It can be solved using case 2 of Master
Theorem.
Average Case:
To do average case analysis, we need to consider all possible permutation of array and
calculate time taken by every permutation which doesn’t look easy.
We can get an idea of average case by considering the case when partition puts O(n/9)
elements in one set and O(9n/10) elements in other set. Following is recurrence for this
case.
Although the worst case time complexity of QuickSort is O(n 2) which is more than many
other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in practice,
because its inner loop can be efficiently implemented on most architectures, and in
most real-world data. QuickSort can be implemented in different ways by changing the
choice of pivot, so that the worst case rarely occurs for a given type of data. However,
merge sort is generally considered better when data is huge and stored in external
storage.
Is QuickSort stable?
The default implementation is not stable. However any sorting algorithm can be made
stable by considering indexes as comparison parameter.
Is QuickSort In-place?
6/11
As per the broad definition of in-place algorithm it qualifies as an in-place sorting
algorithm as it uses extra space only for storing recursive function calls but not for
manipulating the input.
Most practical implementations of Quick Sort use randomized version. The randomized
version has expected time complexity of O(nLogn). The worst case is possible in
randomized version also, but worst case doesn’t occur for a particular pattern (like
sorted array) and randomized Quick Sort works well in practice.
Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference
when used for arrays.
Quick Sort is also tail recursive, therefore tail call optimizations is done.
In arrays, we can do random access as elements are continuous in memory. Let us say
we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i],
we can directly access the memory at (x + i*4). Unlike arrays, we can not do random
access in linked list. Quick Sort requires a lot of this kind of access. In linked list to
access i’th index, we have to travel each and every node from the head to i’th node as we
don’t have continuous block of memory. Therefore, the overhead increases for quick
sort. Merge sort accesses data sequentially and the need of random access is low.
Snapshots:
8/11
References:
http://en.wikipedia.org/wiki/Quicksort
9/11
Recommended Posts:
Stable QuickSort
C++ Program for QuickSort
Why quicksort is better than mergesort ?
Java Program for QuickSort
Dual pivot Quicksort
10/11
Python Program for QuickSort
QuickSort using Random Pivoting
When does the worst case of Quicksort occur?
QuickSort on Singly Linked List
3-Way QuickSort (Dutch National Flag)
Generic Implementation of QuickSort Algorithm in C
QuickSort on Doubly Linked List
Hoare's vs Lomuto partition scheme in QuickSort
Comparisons involved in Modified Quicksort Using Merge Sort Tree
Can QuickSort be implemented in O(nLogn) worst case time complexity?
QuickSort Tail Call Optimization (Reducing worst case space to Log n )
11/11