Quick sort is a popular sorting algorithm that uses a divide-and-conquer approach to sort elements in an array or list. It works by selecting a pivot element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted. In this article, we will explore how to implement the Quick sort algorithm in Python.

## How Quick Sort Works

The Quick sort algorithm works as follows:

- Choose a pivot element from the array. This can be any element, but it is common to choose the first or last element.
- Partition the other elements into two sub-arrays, according to whether they are less than or greater than the pivot.
- Recursively apply the above steps to the sub-arrays.

## Implementing Quick Sort in Python

Now that we know how Quick sort works, let’s look at how we can implement it in Python.

### First Implementation

Here is a simple implementation of Quick sort:

def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in arr[1:]: if i < pivot: left.append(i) else: right.append(i) return quick_sort(left) + [pivot] + quick_sort(right)

Let’s walk through this implementation step-by-step:

- First, we check if the length of the array is less than or equal to 1. If it is, we return the array since it is already sorted.
- If the length of the array is greater than 1, we choose a pivot element. In this case, we choose the first element of the array.
- We create two empty arrays,
`left`

and`right`

. - We iterate over the remaining elements in the array, and for each element, we check if it is less than the pivot. If it is, we append it to the
`left`

array. If it is greater than or equal to the pivot, we append it to the`right`

array. - We then recursively call the
`quick_sort`

function on the`left`

and`right`

sub-arrays, and concatenate the results with the pivot element in the middle.

Let’s test this implementation with an example:

arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_arr = quick_sort(arr) print(sorted_arr)

The output of this code will be:

[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

### Time and Space Complexity

The time complexity of Quick sort is O(n log n) in the average case and O(n^2) in the worst case. The worst case occurs when the pivot element is always the smallest or largest element in the array, resulting in unbalanced partitions.

The space complexity of Quick sort is O(log n) in the average case and O(n) in the worst case. The worst case occurs when the recursion depth is equal to the size of the array.

### Second Implementation

Here is another implementation of Quick sort that uses a different partitioning algorithm:

def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 def quick_sort(arr, low, high): if low < high: pi = partition(arr, low, high) quick_sort(arr, low, pi - 1) quick_sort(arr, pi + 1, high) arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] n = len(arr) quick_sort(arr, 0, n - 1) print(arr)

Let’s walk through this implementation step-by-step:

- We define a
`partition`

function that takes an array, a low index, and a high index as arguments. The pivot element is chosen as the last element in the array. - We initialize
`i`

to be one less than the`low`

index. - We iterate over the array from
`low`

to`high`

, swapping elements that are less than or equal to the pivot with elements that are greater than the pivot. We increment`i`

every time we swap elements. - We swap the pivot element with the element at
`i + 1`

. - We return the index of the pivot element.
- We define a
`quick_sort`

function that takes an array, a low index, and a high index as arguments. If the`low`

index is less than the`high`

index, we partition the array and recursively call`quick_sort`

on the left and right sub-arrays. - We call the
`quick_sort`

function on the entire array to sort it.

Let’s test this implementation with the same example:

arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] n = len(arr) quick_sort(arr, 0, n - 1) print(arr)

The output of this code will be:

[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

### Conclusion

Quick sort is a popular sorting algorithm that is efficient and easy to implement. It works by partitioning an array around a pivot element and recursively sorting the left and right sub-arrays. The choice of pivot element can affect the performance of the algorithm, but there are ways to mitigate this issue. Quick sort has a time complexity of O(n log n) in the average case and O(n^2) in the worst case, and a space complexity of O(log n) in the average case and O(n) in the worst case. There are different partitioning algorithms that can be used in Quick sort, and the implementation we showed in this example uses a simple one that chooses the last element in the array as the pivot.