forked from nryoung/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
33 lines (23 loc) · 680 Bytes
/
Copy pathquick_sort.py
File metadata and controls
33 lines (23 loc) · 680 Bytes
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
"""
quick_sort.py
Implementation of quick sort on a list and returns a sorted list.
Quick Sort Overview:
------------------------
Uses partitioning to recursively divide and sort the list
Time Complexity: O(n**2) worst case
Space Complexity: O(n**2) this version
Stable: No
Psuedo Code: CLRS. Introduction to Algorithms. 3rd ed.
"""
def sort(seq):
if len(seq) <= 1:
return seq
else:
pivot = seq[0]
left, right = [], []
for x in seq[1:]:
if x < pivot:
left.append(x)
else:
right.append(x)
return sort(left) + [pivot] + sort(right)