Skip to content

Commit e0874f7

Browse files
author
Vimal A R
committed
2 parents 01c5a53 + 2246631 commit e0874f7

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎Day-111/quick_sort.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
# Algorithm: Quick sort
4+
# Referrence: https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py
5+
6+
7+
8+
def quick_sort(num_list):
9+
"""
10+
Quick sort in Python
11+
12+
If length of the list is 1 or less, there is no point in sorting it.
13+
Hence, the code works on lists with sizes greater than 1
14+
"""
15+
16+
if len(num_list) <= 1:
17+
return(num_list)
18+
else:
19+
pivot = num_list.pop()
20+
less_than = []
21+
greater_than = []
22+
for num in num_list:
23+
if num < pivot:
24+
less_than.append(num)
25+
elif num > pivot:
26+
greater_than.append(num)
27+
#print(less_than)
28+
#print(pivot)
29+
#print(greater_than)
30+
return quick_sort(less_than) + [pivot] + quick_sort(greater_than)
31+
32+
33+
num_list = [10, -1, 100, 23, 5, 98, 45, 76, -545, -300, 9999]
34+
print(quick_sort(num_list))

0 commit comments

Comments
 (0)