-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort_comp.py
189 lines (173 loc) · 4.7 KB
/
sort_comp.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import time
import matplotlib.pyplot as plt
import numpy as np
from progress.bar import Bar
def bubble_sort(a=[]):#Modified bubble sort
b = a
count = 0
for i in range(len(b)):
swap_done = False
for j in range(0,len(b)-i-1):
if b[j]>b[j+1]:
b[j],b[j+1]=b[j+1],b[j]
swap_done = True
count+=1
if swap_done == False:
break
#print b
return count
def insertion_sort(a = []):
b = a
count = 0
for i in range(1, len(b)):
key = b[i]
j = i-1
while j >=0 and key < b[j] :
b[j+1] = b[j]
j -= 1
count+=1
b[j+1] = key
#print b
return count
def selection_sort(a = []):
b = a
i = len(b)
count = 0
for i in range(len(b)):
j = len(b) - i
pos = i
for j in range(i+1,len(b)):
if b[pos] > b[j]:
pos = j
count += 1
b[i],b[pos] = b[pos],b[i]
#print b
return count
def quick_sort(a = []):
b = a
count = sorter(0,len(b)-1,b)
#print b
return count
def sorter(start_index, end_index,b = []):
if start_index < end_index:
partition_index,count3 = partition(start_index, end_index, b)
count1 = sorter(start_index,partition_index-1,b)
count2 = sorter(partition_index+1,end_index,b)
return count1+count2+count3
else:
return 0
def partition(start_index, end_index,b = []) :
x = b[end_index]
i = start_index-1
count = 0
for j in range(start_index,end_index):
count+=1
if b[j] <= x:
i = i + 1
b[i],b[j] = b[j],b[i]
b[i+1],b[end_index] = b[end_index],b[i+1]
return [i+1,count]
def merge_sort(a = []):
b = a
count = mergeSort(b,0,len(b)-1)
#print b
return count
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r- m
L = [0] * (n1)
R = [0] * (n2)
for i in range(0 , n1):
L[i] = arr[l + i]
for j in range(0 , n2):
R[j] = arr[m + 1 + j]
i = 0
j = 0
k = l
count = 0
while i < n1 and j < n2 :
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
count += 1
else:
arr[k] = R[j]
j += 1
count += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
count += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
count += 1
return count
def mergeSort(arr,l,r):
if l < r:
m = int((l+(r-1))/2)
count_L = mergeSort(arr, l, m)
count_R = mergeSort(arr, m+1, r)
count_merge = merge(arr, l, m, r)
return count_L+count_R+count_merge
else:
return 0
def compare(sort_algo,function_name):
var_of_ops = []
avg_ops = []
avg_time = []
#bar = Bar(function_name,max = 10)
#bar = Bar(function_name,max = 100/2)
bar = Bar(function_name, max = 400/10)
for i in range(1,400,10):
#for i in range(1,100,2): #UNCOMMENT FOR CHANGING INPUT SIZE
#for i in range(0,10):
ops = []
times = []
with open("randoms.txt","r") as randoms_file:
for j in range(0,1000):
a = randoms_file.readline().strip(',\n').split(',')
a = list(map(int,a))
start_time = time.time()
#Number of operations for sorting i numbers
ops.append(sort_algo(a[:i]))
#Actual runtime of algorithm on comp
times.append(time.time()-start_time)
#Average runtime for algo for sorting i numbers
avg_time.append(np.average(times))
#Average number of operatioins for sorting i numbers
avg_ops.append(np.average(ops))
bar.next()
bar.finish()
return [avg_ops,avg_time]
avg_count_bub_sort,avg_time_bub_sort = compare(bubble_sort,'bubble sort')
avg_count_ins_sort,avg_time_ins_sort = compare(insertion_sort,'insertion sort')
avg_count_sel_sort,avg_time_sel_sort= compare(selection_sort,'selection sort')
avg_count_quick_sort,avg_time_quick_sort= compare(quick_sort,'quick sort')
avg_count_merge_sort,avg_time_merge_sort = compare(merge_sort,'merge sort')
'''
plt.plot(avg_count_bub_sort,label='bubble sort')
plt.plot(avg_count_ins_sort,label='insertion sort')
plt.plot(avg_count_sel_sort,label = 'selection sort')
plt.plot(avg_count_quick_sort,label = 'quick sort')
plt.plot(avg_count_merge_sort,label = 'merge sort')
plt.ylabel('average no. of ops')
plt.legend(loc='best')
plt.xlabel('number of elements in array')
plt.show()
'''
plt.plot(avg_time_bub_sort, label = 'bubble_sort')
plt.plot(avg_time_ins_sort, label = 'insertion_sort')
plt.plot(avg_time_sel_sort, label = 'selection_sort')
plt.plot(avg_time_quick_sort, label = 'quick_sort')
plt.plot(avg_time_merge_sort, label = 'merge_sort')
plt.ylabel('avg time of execution')
plt.xlabel('number of elements in array')
#plt.xticks(range(0,60,10),range(0,120,20))
plt.xticks(range(0,45,5),range(0,450,50))
plt.legend(loc='best')
plt.show()