-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_experiment.py
More file actions
60 lines (56 loc) · 1.88 KB
/
Copy pathsort_experiment.py
File metadata and controls
60 lines (56 loc) · 1.88 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
'''
@author-1:Rishab Katta
@author-2:Akhil karrothu
'''
from instrumented_sort import *
import random
import time
import sys
def generate_data(N):
'''
Generate a random sequence of N integers
:param N: a Non-negative Integer
:return:
'''
listOfNumbers=[]
for _ in range(N):
listOfNumbers.append(random.randint(1,N+1000))
return listOfNumbers
def check_sorted(alist):
'''
:param alist: A list containing any number of elements
:return: True if the list is in sorted fashion, False otherwise
'''
for i in range(0,len(alist)-1):
if(alist[i]<=alist[i+1]):
continue
else:
return False
return True
def timeandtablegen():
'''
This function is used to calculate the time taken by the sorting algorithms and also to generate a table
consisting of Number of elements in the list, number of comparisons and time taken by the specific algorithm.
:return: none
'''
if(len(sys.argv)>2 or len(sys.argv)<2):
N=int(input("Please enter the range of numbers"))
else:
N = int(sys.argv[1])
nlist = generate_data(N)
print("\n")
print(nlist)
print("check_sorted before sorting: " + str(check_sorted(nlist)))
sstarttime = time.time()
ssortedlist,scount =ssort(nlist)
stotaltime = (time.time()- sstarttime)
mstarttime=time.time()
msortedlist,mcount = msort(nlist)
mtotaltime=(time.time()-mstarttime)
print(msortedlist)
print("check_sorted after sorting: "+ str(check_sorted(msortedlist)))
print("\n")
print("ALGORITHM \t\t N\t COMPARISONS\t SECONDS" )
print("Selection Sort \t" + str(N)+"\t\t " + str(scount)+"\t\t\t\t " + str(stotaltime))
print("Merge Sort \t" + str(N) + "\t\t " + str(mcount) + "\t\t\t\t " + str(mtotaltime))
timeandtablegen()