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
35 lines (27 loc) · 758 Bytes
/
quick_sort.py
File metadata and controls
35 lines (27 loc) · 758 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
34
35
"""
Quick Sort
----------
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):
"""
Takes a list of integers and sorts them in ascending order. This sorted
list is then returned.
:param seq: A list of integers
:rtype: A list of sorted integers
"""
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)