Skip to content

Commit efb9047

Browse files
committed
Added Quick Sort
1 parent 7aaea7a commit efb9047

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Quicksort Running Time:
3+
Quick sort average case is O(n log n)
4+
each level takes O(n) but splitting the data is O(log n)
5+
O(n) * O(log n) = O(n log n)
6+
Worse case is O(log n2)
7+
if pivot is smallest value, each level is O(n) and splitting the data is O(n)
8+
O(n) * O(n) = O(n2)
9+
"""
10+
11+
# quicksort function
12+
def quicksort(array):
13+
if len(array) < 2:
14+
return array
15+
else:
16+
pivot = array[0]
17+
less = [i for i in array[1:] if i <= pivot]
18+
greater = [i for i in array[1:] if i > pivot]
19+
return quicksort(less) + [pivot] + quicksort(greater)
20+
21+
array = [100, 5, 72, 41, 80, 1, 99, 36, 27, 78]
22+
print(quicksort(array)) # [1, 5, 27, 36, 41, 72, 78, 80, 99, 100]
23+

0 commit comments

Comments
 (0)