-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex2.2.py
47 lines (40 loc) · 1.13 KB
/
ex2.2.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import matplotlib.pyplot as plt
import timeit
import sys
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 main():
with open('ex2.5.json','r') as source:
arr = json.load(source)
x_labels = []
y_labels = []
for i in arr:
x_labels.append(len(i))
y_labels.append(timeit.timeit(lambda: func1(i, 0, (len(i)-1)), number=1))
plt.plot(x_labels, y_labels)
plt.title("Timing results (ex2.2.py)")
plt.xlabel("Size of Sub-Array")
plt.ylabel("Time Taken (seconds)")
plt.show()
if __name__ == '__main__':
main()