Practice code with the "Quick Sort" algorithm
The principle of Quick sort algorithm
Quick sort is an efficient sorting algorithm. Here’s how it works:
- Choose a pivot: select an element from the array (commonly the first, last, or a random element).
- Partition: rearrange the array so elements less than the pivot come before it, and elements greater come after. The pivot is now in its final position.
- Recursively sort: apply the same process to the sub-arrays on either side of the pivot.
- Base case: stop when sub-arrays have one or zero elements, as they are already sorted.
Example
Let’s go through Quick Sort step-by-step with the example array [5, 3, 7, 6, 2, 1, 8]
.
- Initial Array:
[5, 3, 7, 6, 2, 1, 8]
- Pivot:
5
(first element)
Step 1: Partitioning
- Elements less than
5
:[3, 2, 1]
- Elements equal to
5
:[5]
- Elements greater than
5
:[7, 6, 8]
The array now looks like: [3, 2, 1] [5] [7, 6, 8]
Step 2: Recursively sort sub-arrays
Left sub-array [3, 2, 1]
:
- Pivot:
3
- Partitioning:
- Elements less than
3
:[2, 1]
- Elements equal to
3
:[3]
- Elements greater than
3
:[]
- Elements less than
The array now looks like: [2, 1] [3] []
Right sub-array [7, 6, 8]
:
- Pivot:
7
- Partitioning:
- Elements less than
7
:[6]
- Elements equal to
7
:[7]
- Elements greater than
7
:[8]
- Elements less than
The array now looks like: [6] [7] [8]
Step 3: Recursively sort again
Left sub-array [2, 1]
:
- Pivot:
2
- Partitioning:
- Elements less than
2
:[1]
- Elements equal to
2
:[2]
- Elements greater than
2
:[]
- Elements less than
The array now looks like: [1] [2] []
[6] [7] [8]
is already sorted.
Step 4: Combine sorted arrays
- Left part:
[1, 2, 3]
- Middle part:
[5]
- Right part:
[6, 7, 8]
Final sorted array: [1, 2, 3, 5, 6, 7, 8]
The naive simple version
This version of quicksort correctly implements the core idea but differs from the “classic” quicksort in a few ways:
- Classic quicksort sorts the array in place, without using additional lists (
smaller_elements
andlarger_elements
). It rearranges elements within the original array to partition around the pivot.
The real version
A closer version of quick sort would look like this:
Alternatives
- Pivot selection: while choosing the first or last element as the pivot is a valid approach, it’s not the only one. The median-of-three method, which selects the median of the first, middle, and last elements, is often used to improve performance and reduce the chance of encountering worst-case scenarios.
- Two-pointer technique: classic quicksort does indeed use a two-pointer technique. One pointer starts at the beginning and the other at the end, moving towards each other to partition the array. This method can improve efficiency by reducing the number of swaps needed.
Complexity
The time complexity of quicksort can vary depending on how the pivot is chosen and the input array’s characteristics:
-
Best case: O(n log n): this occurs when the pivot is chosen in such a way that it divides the array into two nearly equal halves each time.
-
Average case: O(n log n): in most practical scenarios, quicksort performs close to its best case due to its efficient partitioning and divide-and-conquer approach.
-
Worst case: O(n²): this happens when the pivot is chosen poorly, resulting in highly unbalanced partitions. An example is when the smallest or largest element is always chosen as the pivot, leading to unbalanced recursion.
Sources
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create a Docker Swarm playground
Let's create Docker Swarm playground on your local machine
Create an Ansible playground with Docker
Let's create an Ansible playground with Docker
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
Setup a Kubernetes cluster with K3S, Traefik, CertManager and Kubernetes Dashboard
Let's setup step by step our own K3S cluster !
How to internationalize an AstroJS website while maintaining good SEO ?
We will see how to create an implementation of i18n with AstroJS
LFTP - Deploy an application in command line
Here we will see how to automatically deploy an application with lftp in command line.
Create a WAMP environment with docker
An easy code to install Apache, MySQL and PHP with Docker !