-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
40 lines (32 loc) · 1.68 KB
/
Copy pathquick_sort.py
File metadata and controls
40 lines (32 loc) · 1.68 KB
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
36
37
38
39
40
''' Quick Sort Algorithm
this algorithm orders an unordered list of elements by picking a pivot
element within the list and compare each and every element within the
list to break the original list into 2 lists, larger than pivot and
less than pivot, this runs recursively until the smallest list is 1
then every list is combined to form the ordered list
#####################################################################
the trick of this quick sort algorithm is cleverly picking the pivot,
worst case is when extreams are selected as the pivot'''
def quick_sort(sequence):
# length here returns the length of our data sequence
length = len(sequence)
# if the lenght of list is of a single element just return the element
if length <= 1:
return sequence
else:
# we label one of the element in the list "pivot" in order to compare
# other elements to this pivot element to position accordingly
pivot = sequence.pop()
# here we define 2 lists, one list contains elements less than pivot from
# our original sequence, other list contain elements greater than pivot
items_greater = []
items_lower = []
for item in sequence: # passing through our sequence
if item > pivot: # compare each elements in our sequence with pivot
# add element to items_greater if larger than pivot
items_greater.append(item)
else:
# add element to items_lower if smaller or equal than pivot
items_lower.append(item)
# pass sequence recursively until length is 1 and combine every list
return quick_sort(items_lower) + [pivot] + quick_sort(items_greater)