-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort
84 lines (66 loc) · 2.9 KB
/
Quicksort
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# sorts data by dividing into smaller lists using a partition and a random element to base greater than and less than
#Base case (I built)
from random import randrange, shuffle
def quicksort(list, start, end):
# this portion of listay has been sorted
if start >= end:
return
# select random element to be pivot
pivot_idx = randrange(start, end + 1)
pivot_element = list[pivot_idx]
# swap random element with last element in sub-listay
list[end], list[pivot_idx] = list[pivot_idx], list[end]
# tracks all elements which should be to left (lesser than) pivot
less_than_pointer = start
for i in range(start, end):
# we found an element out of place
if list[i] < pivot_element:
# swap element to the right-most portion of lesser elements
list[i], list[less_than_pointer] = list[less_than_pointer], list[i]
# tally that we have one more lesser element
less_than_pointer += 1
# move pivot element to the right-most portion of lesser elements
list[end], list[less_than_pointer] = list[less_than_pointer], list[end]
# Call quicksort on the "left" and "right" sub-lists
quicksort(list,start,less_than_pointer - 1)
quicksort(list, less_than_pointer +1,end)
unsorted_list = [3,7,12,24,36,42]
shuffle(unsorted_list)
print(unsorted_list)
# use quicksort to sort the list, then print it out!
quicksort(unsorted_list,0,len(unsorted_list)-1)
print(unsorted_list)
#CodeAcademy case with extra print statements describing what is going on
from random import randrange, shuffle
def quicksort(list, start, end):
# this portion of list has been sorted
if start >= end:
return
print("Running quicksort on {0}".format(list[start: end + 1]))
# select random element to be pivot
pivot_idx = randrange(start, end + 1)
pivot_element = list[pivot_idx]
print("Selected pivot {0}".format(pivot_element))
# swap random element with last element in sub-lists
list[end], list[pivot_idx] = list[pivot_idx], list[end]
# tracks all elements which should be to left (lesser than) pivot
less_than_pointer = start
for i in range(start, end):
# we found an element out of place
if list[i] < pivot_element:
# swap element to the right-most portion of lesser elements
print("Swapping {0} with {1}".format(list[i], pivot_element))
list[i], list[less_than_pointer] = list[less_than_pointer], list[i]
# tally that we have one more lesser element
less_than_pointer += 1
# move pivot element to the right-most portion of lesser elements
list[end], list[less_than_pointer] = list[less_than_pointer], list[end]
print("{0} successfully partitioned".format(list[start: end + 1]))
# recursively sort left and right sub-lists
quicksort(list, start, less_than_pointer - 1)
quicksort(list, less_than_pointer + 1, end)
list = [5,3,1,7,4,6,2,8]
shuffle(list)
print("PRE SORT: ", list)
print(quicksort(list, 0, len(list) -1))
print("POST SORT: ", list)