-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.4.py
More file actions
64 lines (56 loc) · 1.74 KB
/
ex2.4.py
File metadata and controls
64 lines (56 loc) · 1.74 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
import matplotlib.pyplot as plt
import sys
import json
import time
import colorsys as c
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)
return arr
def func2(array, start, end):
p = array[random.randint(start, end)]
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.json') as f:
data = json.load(f)
x = [len(data[i]) for i in range(len(data))]
y = []
for i in range(len(data)):
start_time = time.time()
if len(data[i]) <= 1000:
data[i] = func1(data[i], 0, len(data[i])-1)
else:
partitions = len(data[i]) // 1000 + 1
temp = []
for j in range(partitions):
start = j * 1000
end = min(start + 1000, len(data[i]))
partition = data[i][start:end]
temp.extend(func1(partition, 0, len(partition)-1))
data[i] = temp
y.append(time.time() - start_time)
plt.plot(x, y)
plt.xlabel('Input Size')
plt.ylabel('Time (seconds)')
plt.title('Time Complexity Plot')
plt.show()
#with open('ex2.5.json', 'w') as f:
#json.dump(data, f)
if __name__ == "__main__":
main()