Repository navigation
Expand file tree
/
Copy pathquick_sort.py
More file actions
52 lines (36 loc) · 1.42 KB
/
Copy pathquick_sort.py
File metadata and controls
52 lines (36 loc) · 1.42 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from random import randint
from timeit import repeat
from random import randint
def quick_sort(array):
# Base cases
if len(array) < 2:
return array
low, same, high = [], [], []
# Select the pivot randomly
pivot = array[randint(0, len(array) - 1)]
for item in array:
# Elements smaller than the pivot go to the low list
if item < pivot:
low.append(item)
# Elements equal to pivot go to the same list.
elif item == pivot:
same.append(item)
# Elements larger than pivot go to the high list.
elif item > pivot:
high.append(item)
# The final result combines the sorted low, same, sorted
return quick_sort(low) + same + quick_sort(high)
# Test cases
# Performance evaluation
def check_performance(algorithm):
ARRAY_LENGTH = 1000
# Generate an array of `ARRAY_LENGTH` random integer between 0 and 999
array = [randint(0, 1000) for i in range(ARRAY_LENGTH)]
# Execute the code ten times and return the time that each execution took
stmt = f"{algorithm}({array})"
setup_code = f"from __main__ import {algorithm}"
times = repeat(setup=setup_code, stmt=stmt, repeat=3, number=10)
# Display the name of the algorithm and the minimum time it took to run
print(f"Algorithm: {algorithm}, minimum execution time: {min(times)}")
if __name__ == "__main__":
check_performance("quick_sort")