You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algorithms/explanations/QSExp.md
+35-17Lines changed: 35 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -6,35 +6,53 @@ Quicksort is a divide and conquer algorithm. It first rearranges the input
6
6
array into two smaller sub-arrays: the (relatively) low elements and the
7
7
(relatively) high elements. It then recursively sorts each of the sub-arrays.
8
8
9
-
### Sorting using Quicksort
9
+
##Algorithm overview
10
10
11
-
The steps for Quicksort are:
11
+
The steps for basic Quicksort are:
12
12
13
-
* Pick the rightmost element of the array, called a pivot.
13
+
* Pick the *pivot* element of the sub-array; here it is the rightmost
14
+
element.
14
15
15
-
* Partitioning: reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it. After this partitioning, the pivot is in its final position.
16
+
* Partitioning: reorder the sub-array so that only elements with values less than or equal to the pivot come before the pivot, while only elements with values greater than or equal to the pivot come after it. After this partitioning, the pivot is in its final position.
16
17
17
-
* Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
18
+
* Recursively apply the above steps to the sub-array of elements before the pivot and separately to the sub-array of elements after the pivot.
18
19
19
-
The base case of the recursion is arrays of size one or zero, which are in order by definition, so they never need to be sorted.
20
+
The base case of the recursion is sub-arrays of size one or zero, which are in order by definition, so they never need to be sorted.
20
21
22
+
## Partitioning
21
23
22
-
### Complexity
24
+
The way partitioning is done here is to use two pointers/indices to
25
+
scan through the sub-array. One starts at the left and scans right
26
+
in search for "large" elements (greater than or equal to the pivot).
27
+
The other starts at the right and scans left in search for "small"
28
+
elements (less than or equal to the pivot). Whenever a large and a small
29
+
element are found they are swapped. When the two indices meet, the pivot
30
+
is swapped into that position and partitioning is complete.
23
31
24
-
Time complexity:
25
-
<code>
26
-
Average case <i>O(n log n)</i>
27
-
Worst case <i>O(n<sup>2</sup>)</i>
28
-
Best case <i>O(n log n)</i>
29
32
30
-
Note: Worst case in quicksort occurs when a file is already sorted, since the partition is highly asymmetrical. Improvements such as median-of-three quicksort make a significant improvement, although worst case behaviour is still possible.
31
-
</code>
33
+
## Time complexity
32
34
33
-
Space complexity is O(1), that is, no extra space is required.
35
+
In the best case, partition divides the sub-array in half at each step,
36
+
resulting in <i>O(log n)</i> levels of recursion and <i>O(n log n)</i>
37
+
complexity overall. In the worst case, partition divides the sub-array
38
+
very unevenly at each step. The pivot element is either the largest or
39
+
smallest element in the sub-array and one of the resulting partitions
40
+
is always empty, resulting in <i>O(n<sup>2</sup>)</i> complexity.
41
+
This occurs if the input is sorted or reverse-sorted. Refinements such
42
+
as median of three partitioning (shown elsewhere) make the worst case
43
+
less likely. On average, partitioning is reasonably well balanced and
44
+
<i>O(n log n)</i> complexity results.
34
45
46
+
## Space complexity
35
47
48
+
Although there is no explicit additional space required, quicksort is
49
+
recursive, so it uses implicit stack space proportional to the depth of
50
+
recursion. The best and average cases are <i>O(log n)</i> but the worst
51
+
case is <i>O(n)</i>.
36
52
37
-
### Development of Quicksort
53
+
54
+
55
+
## Development of Quicksort
38
56
39
57
The first version of quicksort was published by Tony Hoare in 1961 and
40
58
quicksort remains the *fastest* sorting algorithm on average (subject to
@@ -43,4 +61,4 @@ done in *many* different ways and the choice of specific implementation
43
61
details and computer hardware can significantly affect the algorithm's
44
62
performance. In 1975, Robert Sedgewick completed a Ph.D. thesis on this
45
63
single algorithm. Our presentation here is influenced by the original
46
-
Hoare version and some of Sedgewick's adaptations.
64
+
Hoare version and some of Sedgewick's adaptations.
0 commit comments