🌑

Stephen Cheng

Quicksort Algorithm

 

Stephen Cheng

Intro

The Quicksort algorithm is one of the fastest sorting algorithms. It takes an array of values, chooses one of the values as the ‘pivot’ element, and moves the other values so that lower values are on the left of the pivot element, and higher values are on the right of it.

In this tutorial the last element of the array is chosen to be the pivot element, but we could also have chosen the first element of the array, or any element in the array really. Then, the Quicksort algorithm does the same operation recursively on the sub-arrays to the left and right side of the pivot element. This continues until the array is sorted.

How it works?

  • Choose a value in the array to be the pivot element.
  • Order the rest of the array so that lower values than the pivot element are on the left, and higher values are on the right.
  • Swap the pivot element with the first element of the higher values so that the pivot element lands in between the lower and higher values.
  • Do the same operations (recursively) for the sub-arrays on the left and right side of the pivot element.

Manual Run Through

Step 1: We start with an unsorted array.

1
[ 11, 9, 12, 7, 3]

Step 2: We choose the last value 3 as the pivot element.

1
[ 11, 9, 12, 7, 3]

Step 3: The rest of the values in the array are all lower than 3, an must be on the right side of 3. Swap 3 with 11.

1
[ 3, 9, 12, 7, 11]

Step 4: Value 3 is now in the correct position. We need to sort the values to the right of 3. We choose the last value 11 as the new pivot element.

1
[ 3, 9, 12, 7, 11]

Step 5: The value 7 must be to the left of pivot value 11, and 12 must be to the right of it. Move 7 and 12.

1
[ 3, 9, 7, 12, 11]

Step 6: Swap 11 with 12 so that lower values 9 anf 7 are on the left side of 11, and 12 is on the right side.

1
[ 3, 9, 7, 11, 12]

Step 7: 11 and 12 are in the correct positions. We choose 7 as the pivot element in sub-array [ 9, 7], to the left of 11.

1
[ 3, 9, 7, 11, 12]

Step 8: We must swap 9 with 7.

1
[ 3, 7, 9, 11, 12]

Finally, the array is sorted.

Implementation

To implement the Quicksort algorithm in a programming language, we need:

  1. An array with values to sort.
  2. A quickSort method that calls itself (recursion) if the sub-array has a size larger than 1.
  3. A partition method that receives a sub-array, moves values around, swaps the pivot element into the sub-array and returns the index where the next split in sub-arrays happens.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def partition(array, low, high):
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= pivot:
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=0, high=None):
step = 0
if high is None:
high = len(array) - 1

if low < high:
pivot_index = partition(array, low, high)
print("The sorted array after {0} step: ".format(low+1), array)
quicksort(array, low, pivot_index-1)
quicksort(array, pivot_index+1, high)

my_array = [64, 34, 25, 12, 22, 11, 90, 5]
quicksort(my_array)
print("Sorted array:", my_array)

Time Complexity

The worst case scenario for Quicksort is O(nn). This is when the pivot element is either the highest or lowest value in every sub-array, which leads to a lot of recursive calls. With our implementation above, this happens when the array is already sorted But on average, the time complexity for Quicksort is actually just O(nlogn).

The recursion part of the Quicksort algorithm is actually a reason why the average sorting scenario is so fast, because for good picks of the pivot element, the array will be split in half somewhat evenly each time the algorithm calls itself. So the number of recursive calls do not double, even if the number of values n double.

, , — Feb 16, 2022

Search

    Made with ❤️ and ☀️ on Earth.