forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.py
27 lines (23 loc) · 803 Bytes
/
qsort.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
"""
High level explanation:
Quicksort algorithm is that if we can efficiently partition a list,
then we can efficiently sort it. Partitioning a list means that
we pick a pivot item in the list, and then modify the list
to move all items larger than the pivot to the right and all
smaller items to the left.
Once the pivot is done, we can do the same operation to the
left and right sections of the list recursively until the list is sorted.
Time complexity:
Quicksort has a time complexity of O(n log n).
"""
def qsort(arr):
if len(arr) <= 1:
return arr
pivot = arr.pop()
greater, lesser = [], []
for item in arr:
if item > pivot:
greater.append(item)
else:
lesser.append(item)
return qsort(lesser) + [pivot] + qsort(greater)