-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.4.py
More file actions
78 lines (66 loc) · 1.94 KB
/
Copy pathex2.4.py
File metadata and controls
78 lines (66 loc) · 1.94 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 json
import time
import matplotlib.pyplot as plt
import urllib.request
import random
sys.setrecursionlimit(20000)
def func1(arr, low, high):
if low < high:
pi = func2(arr, low, high)
func1(arr, low, pi-1)
func1(arr, pi + 1, high)
# def func2(array, start, end):
# p = array[start]
# low = start + 1
# high = end
# while True:
# while low <= high and array[high] >= p:
# high = high - 1
# while low <= high and array[low] <= p:
# low = low + 1
# if low <= high:
# array[low], array[high] = array[high], array[low]
# else:
# break
# array[start], array[high] = array[high], array[start]
# return high
def func2(arr, low, high):
i = (low - 1)
pivot = arr[int((low + high) / 2)]
for j in range(low, high):
if arr[j] <= pivot:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1)
def test_quick_sort(inputs):
results = []
for arr in inputs:
start = time.time()
func1(arr, 0, len(arr)-1)
end = time.time()
results.append(end - start)
print(results)
return results
def plot_results(results):
x = list(range(0, len(results)))
plt.plot(x, results, label="Quick Sort")
plt.xlabel("Input (n)")
plt.ylabel("Time (s)")
plt.title("Comparison of Quick Sort Times")
plt.legend()
plt.show()
def fetch_inputs():
url = ("https://raw.githubusercontent.com/ldklab/ensf338w23/main/assignments/assignment2/ex2.json")
response = urllib.request.urlopen(url)
inputs = json.loads(response.read().decode())
# randomize the inputs
for i in range(len(inputs)):
random.shuffle(inputs[i])
return inputs
if __name__ == "__main__":
inputs = fetch_inputs()
results = test_quick_sort(inputs)
print(results)
plot_results(results)