-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.py
28 lines (24 loc) · 909 Bytes
/
QuickSort.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
28
"""
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array
and partitioning the other elements into two sub-arrays, according to whether they are less than or
greater than the pivot.
"""
class QuickSort:
def quick_sort(self, arr_list):
if len(arr_list) <= 1:
return arr_list
pivot = arr_list.pop()
items_lower = []
items_greater = []
for item in arr_list:
if item < pivot:
items_lower.append(item)
else:
items_greater.append(item)
return self.quick_sort(items_lower) + [pivot] + self.quick_sort(items_greater)
if __name__ == "__main__":
quicksort = QuickSort()
array = input("Enter space separated unordered numbers:").split()
array = list(map(int, array))
sorted_array = quicksort.quick_sort(array)
print(sorted_array)