Python Program for QuickSort
Just unlikely merge Sort, QuickSort is a divide and conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot.
There are many different versions of quickSort that pick pivot in different ways.
- Always pick the first element as a pivot
- Always pick the last element as a pivot
- Pick a random element as a pivot
- Pick median as a pivot
Here we will be picking the last element as a pivot. The key process in quickSort is partition(). Target of partitions is, given an array and an element ‘x’ of array as a pivot, put x at its correct position in a 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.
Python Recursive QuickSort function
// low --> Starting index,
// high --> Ending index
quickSort(arr[], low, high) {
// Till starting index is lesser than ending index
if (low < high) {
// pi is partitioning index,
// arr[p] is now at right place
pi = partition(arr, low, high);
// Before pi
quickSort(arr, low, pi - 1);
// After pi
quickSort(arr, pi + 1, high);
}
}
Python3
def partition(array, low, high):
pivot = array[high]
i = low - 1
for j in range (low, high):
if array[j] < = pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1 ], array[high]) = (array[high], array[i + 1 ])
return i + 1
def quickSort(array, low, high):
if low < high:
pi = partition(array, low, high)
quickSort(array, low, pi - 1 )
quickSort(array, pi + 1 , high)
data = [ 1 , 7 , 4 , 1 , 10 , 9 , - 2 ]
print ( "Unsorted Array" )
print (data)
size = len (data)
quickSort(data, 0 , size - 1 )
print ( 'Sorted Array in Ascending Order:' )
print (data)
|
Output
Unsorted Array
[1, 7, 4, 1, 10, 9, -2]
Sorted Array in Ascending Order:
[-2, 1, 1, 4, 7, 9, 10]
Time Complexity: Worst case time complexity is O(N2) and average case time complexity is O(N log N)
Auxiliary Space: O(1)
Quicksort using list comprehension is a recursive algorithm for sorting an array of elements. It works by selecting a pivot element and partitioning the array around the pivot, such that all elements less than the pivot are moved to its left and all elements greater than the pivot are moved to its right. Then, it recursively applies the same process to the left and right sub-arrays until the entire array is sorted.
Algorithm:
1.If the input array has length 0 or 1, return the array as it is already sorted.
2.Choose the first element of the array as the pivot element.
3.Create two empty lists, left and right.
4.For each element in the array except for the pivot:
a. If the element is smaller than the pivot, add it to the left list.
b. If the element is greater than or equal to the pivot, add it to the right list.
5.Recursively call quicksort on the left and right lists.
6.Concatenate the sorted left list, the pivot element, and the sorted right list.
7.Return the concatenated list.
Python3
def quicksort(arr):
if len (arr) < = 1 :
return arr
else :
pivot = arr[ 0 ]
left = [x for x in arr[ 1 :] if x < pivot]
right = [x for x in arr[ 1 :] if x > = pivot]
return quicksort(left) + [pivot] + quicksort(right)
arr = [ 1 , 7 , 4 , 1 , 10 , 9 , - 2 ]
sorted_arr = quicksort(arr)
print ( "Sorted Array in Ascending Order:" )
print (sorted_arr)
|
Output
Sorted Array in Ascending Order:
[-2, 1, 1, 4, 7, 9, 10]
Time complexity is O(n log n)
The space complexity of the algorithm is O(n)
Last Updated :
28 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...