-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.py
More file actions
77 lines (58 loc) · 2.34 KB
/
QuickSort.py
File metadata and controls
77 lines (58 loc) · 2.34 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
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
import sys
import time
import random
from datetime import datetime
from Constants import *
# Utilize Tkinter library to generate graph animation
if sys.version_info[0] == 3:
from tkinter import * # Python v.3+
else:
from Tkinter import * # Python v.2+
# Utility method to sort elements in array in the correct order by placing any elements that are smaller than or equal to the pivot on the left side,
# and any elements that are greater than the pivot on the right side.
def partition(inputArray, start, end):
i = j = start
while j < end:
if inputArray[j] <= inputArray[end]:
inputArray[i], inputArray[j] = inputArray[j], inputArray[i] # swap
i += 1
j += 1
inputArray[i], inputArray[end] = inputArray[end], inputArray[i] # place pivot element in its proper order
return i
def quickSortRecursive(inputArray, start, end):
# base case
if start >= end:
return # done traversing array (elements are sorted)
# partition the input array into two parts: less than pivot value and greater or equal to pivot value
pivotLocation = partition(inputArray, start, end)
quickSortRecursive(inputArray, start, pivotLocation - 1) # sort elements before pivot
quickSortRecursive(inputArray, pivotLocation + 1, end) # sort elements after pivot
print(inputArray)
animation()
cv.delete(ALL)
cv.update()
def quicksort(inputArray):
return quickSortRecursive(inputArray, 0, len(inputArray) - 1)
def animation():
startingPositionX = 100 # starting position on x-axis of the first bar
startingPositionY = 700 # bottom left of the first bar
for item in randomData:
cv.delete([item])
bar = cv.create_rectangle(startingPositionX, startingPositionY, startingPositionX + barWidth,
startingPositionY - (item * barWidth), fill=barColor)
startingPositionX += barWidth + barSpace
time.sleep(pauseTime)
cv.update()
# Canvas
root = Tk()
root.title(title)
cv = Canvas(width=canvasWidth, height=canvasHeight, bg=backgroundColor)
cv.pack()
cv.update()
# Record runtime of the algorithm based on the provided input array
startTime = datetime.now()
quicksort(randomData)
endTime = datetime.now()
print("Sorted list of size {} in {} seconds".format(len(randomData), endTime - startTime))
animation()
root.mainloop()