-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
143 lines (96 loc) · 3.08 KB
/
main.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
# create objects rferencing lists to class
from timeit import default_timer as timer
import searchclass
import sortingclass
import random
import copy
import collections
# Generate at least 10000 random numbers and store those in a list (name the list as Pool
pool = []
for i in range(0, 10000):
n = random.randint(0, 10000)
pool.append(n)
print("Og Pool: ", pool)
# creating copies of the list to be used for sorting methods
pool2 = copy.deepcopy(pool)
pool3 = copy.deepcopy(pool)
pool4 = copy.deepcopy(pool)
# Generate a random number and call it as Target (use random.randint()).
target = random.randint(0, 10000)
print("Target: ", target)
''' Perform Linear Search and Binary Search to find Target in Pool. write each as a function.
Note: Binary search can be performed on sorted integers. Perform a sorting (call a
sorting function) before calling binary search
'''
# sorted pool
spool = sorted(pool)
# feeding it to a search object
poolsearch = searchclass.Search()
# not wise to print a 50,000 long list
print("Sorted list: ", spool)
def linearsearch(l, t):
print("\nLinear Search:")
if poolsearch.sequentialSearch(l, t) == -1:
print("element not found")
else:
print("Target:", target, "found")
def binsearch(l, t):
print("\nBinary Search:")
if not poolsearch.binarySearch(l, t):
print("element not found")
else:
print("Target:", target, "found")
'''Perform Max/Min number search on the unsorted list. write each as a function.'''
def minval(lst):
return poolsearch.minValue(lst)
def maxval(lst):
return poolsearch.maxValue(lst)
'''
Find/Search whether the unsorted list have distinct numbers. If it have distinct numbers
return true and return false otherwise. If false, print the numbers which are not
distinct. write it as a function
'''
def distinct(l):
return poolsearch.distinctValues(l)
def is_distinct(l):
if not poolsearch.distinctValues(l):
print("Repeated Values: ", [item for item, count in collections.Counter(l).items() if count > 1])
# calls for the first set of problems
linearsearch(spool, target)
binsearch(spool, target)
print("\nMin: ", minval(pool))
print("\nMax: ", maxval(pool))
print("\nIs it distinct?", distinct(pool))
print(is_distinct(pool))
'''
Perform the following sorting on the Pool (write each as a function)
'''
sortingpool = sortingclass.SortingClass()
# selection sort
def selectionsort(l):
sortingpool.selectionSort(l)
print("Selection sorted list: ")
selectionsort(pool)
# print("Selection sorted list: ", pool)
# insertion sort
def insersort(l):
sortingpool.insertionSort(l)
print("Insertion sorted list: ")
insersort(pool2)
# print("Insertion sorted list: ", pool2)
# bubble sort
def bubblesort(l):
sortingpool.bubbleSort(l)
print("Bubble sorted list: ")
bubblesort(pool3)
# print("Bubble sorted list: ", pool3)
# merge sort
def mergesort(l):
start = timer()
sortingpool.mergeSort(l)
end = timer()
time = end - start
print("Elapsed time: ", time, "ms")
print("Merge sorted list: ")
mergesort(pool4)
# print("Merge sorted list: ", pool4)