forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
23 lines (20 loc) · 701 Bytes
/
quick_sort.py
File metadata and controls
23 lines (20 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
Quicksort Running Time:
Quick sort average case is O(n log n)
each level takes O(n) but splitting the data is O(log n)
O(n) * O(log n) = O(n log n)
Worse case is O(log n2)
if pivot is smallest value, each level is O(n) and splitting the data is O(n)
O(n) * O(n) = O(n2)
"""
# quicksort function
def quicksort(array):
if len(array) < 2:
return array
else:
pivot = array[0]
less = [i for i in array[1:] if i <= pivot]
greater = [i for i in array[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greater)
array = [100, 5, 72, 41, 80, 1, 99, 36, 27, 78]
print(quicksort(array)) # [1, 5, 27, 36, 41, 72, 78, 80, 99, 100]