-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.py
36 lines (33 loc) · 1.15 KB
/
quick_sort.py
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
"""
|-----------------------------------------------------------|
| Quick Sort |
|-----------------------------------------------------------|
|Type || Comparison sort |
|Worst case performance || O(n2) |
|Best case performance || O(n log n) or O(n) |
|Average case performance || O(n log n) |
|Worst case space complexity || O(n) or O(log n) |
|-----------------------------------------------------------|
"""
from decorators import timethis
@timethis
def quick_sort(array):
def _sort(array):
# Actual Quick Sort Function
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
else:
greater.append(x)
return _sort(less)+equal+_sort(greater)
else:
return array
# Call the Quick Sort Function
return _sort(array[:])