Skip to content

Commit 26c4bce

Browse files
authored
Update quicksort.py
1 parent 6005643 commit 26c4bce

File tree

1 file changed

+0
-17
lines changed

1 file changed

+0
-17
lines changed

divide_and_conquer/quicksort.py

-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
from __future__ import annotations
2-
3-
42
def partition(array: list, low: int, high: int) -> int:
53
"""Helper function for quicksort to partition the array.
6-
74
>>> array = [3, 2, 1]
85
>>> partition(array, 0, len(array) - 1)
96
0
107
>>> array
118
[1, 2, 3]
12-
139
>>> array = [12, 4, 5, 2, 3]
1410
>>> idx = partition(array, 0, len(array) - 1)
1511
>>> array[:idx], array[idx], array[idx+1:]
@@ -25,48 +21,35 @@ def partition(array: list, low: int, high: int) -> int:
2521

2622
array[i + 1], array[high] = array[high], array[i + 1]
2723
return i + 1
28-
29-
3024
def quicksort(array: list, low: int = 0, high: int | None = None) -> list:
3125
"""Returns a sorted list using the quicksort algorithm.
32-
3326
>>> from random import shuffle
3427
>>> array = [-2, 3, -10, 11, 99, 100000, 100, -200]
3528
>>> shuffle(array)
3629
>>> quicksort(array)
3730
[-200, -10, -2, 3, 11, 99, 100, 100000]
38-
3931
>>> shuffle(array)
4032
>>> quicksort(array)
4133
[-200, -10, -2, 3, 11, 99, 100, 100000]
42-
4334
>>> quicksort([-200])
4435
[-200]
45-
4636
>>> array = [-2, 3, -10, 11, 99, 100000, 100, -200]
4737
>>> shuffle(array)
4838
>>> sorted(array) == quicksort(array)
4939
True
50-
5140
>>> quicksort([])
5241
[]
53-
5442
>>> array = [10000000, 1, -1111111111, 101111111112, 9000002]
5543
>>> sorted(array) == quicksort(array)
5644
True
5745
"""
5846
if high is None:
5947
high = len(array) - 1
60-
6148
if low < high:
6249
pivot_index = partition(array, low, high)
6350
quicksort(array, low, pivot_index - 1)
6451
quicksort(array, pivot_index + 1, high)
65-
6652
return array
67-
68-
6953
if __name__ == "__main__":
7054
import doctest
71-
7255
doctest.testmod()

0 commit comments

Comments
 (0)