-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgo.py
More file actions
165 lines (132 loc) · 3.79 KB
/
algo.py
File metadata and controls
165 lines (132 loc) · 3.79 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
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
#!/usr/bin/env python3
# Addie Bendory, Python Code Samples
import random
array = [2,4,5,7,9,10]
def binarySearch(alist, item):
first = 0
last = len(alist) - 1
found = False
while first <= last and not found:
mid = (first + last) // 2
if alist[mid] == item:
found = True
elif item < alist[mid]:
last = mid - 1
else:
first = mid + 1
return found
def bubbleSort(alist):
list_sorted = False
while not list_sorted:
list_sorted = True
for cur in range(len(alist) - 1):
if alist[cur] > alist[cur + 1]:
list_sorted = False
alist[cur], alist[cur + 1] = alist[cur + 1], alist[cur]
def selectionSort(alist):
for cur in range(len(alist)):
min_pos = cur
for list_pos in range(cur + 1,len(alist)):
if (alist[list_pos] < alist[min_pos]):
min_pos = list_pos
if alist[cur] != alist[min_pos]:
alist[cur], alist[min_pos] = alist[min_pos], alist[cur]
def insertionSort(alist, start=0, gap=1):
for cur in range(start+gap, len(alist), gap):
current_value = alist[cur]
pos = cur
while (pos >= gap and alist[pos-gap] > current_value): # scan from right to left
alist[pos] = alist[pos-gap]
pos -= gap
alist[pos] = current_value
def shellSort(alist):
sublist = len(alist) // 2
while sublist > 0:
for start in range(sublist):
insertionSort(alist, start, sublist)
sublist //= 2
def mergeSort(alist):
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=j=k=0 # lefthalf (i), righthalf (j), and alist (k) indices
while (i < len(lefthalf) and j < len(righthalf)):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
def heapSort(alist):
pass
def quickSort(alist):
# set a pivot to first element
pivot = alist[0]
left = alist[1]
right = alist[-1]
if left < pivot: pivot = alist[1]
def quick3Sort(alist):
pass
class Vertex:
def __init__(self,key):
self.id = key
self.connectedTo = {}
##################### TEST FUNCTIONS ####################
def create_random():
alist = [random.randrange(100) for i in range(10)]
return alist
def print_list(alist):
for item in alist:
print("{:3}".format(item), end="")
print()
def print_sort(sort, alist):
print ("Random List: ", end=""); print_list(alist)
sort(alist)
print ("Sorted List: ", end=""); print_list(alist)
print ("*"*43)
def print_search(search, alist, item):
print ("Random List: ", end=""); print_list(alist)
print ("Searching for " + str(item))
found = search(alist, item)
print ("Was item found? " + str(found))
print ("*"*43)
# Run the algorithms
print ("Binary Search")
print ("-------------")
print_search(binarySearch, sorted([22,41,57,11,68,83,36,70,45]), random.randrange(100))
print ("Bubble Sort")
print ("-----------")
print_sort(bubbleSort, create_random())
print ("Selection Sort")
print ("--------------")
print_sort(selectionSort, create_random())
print ("Insertion Sort")
print ("--------------")
print_sort(insertionSort, create_random())
print ("Shell Sort")
print ("----------")
print_sort(shellSort, create_random())
print ("Merge Sort")
print ("----------")
print_sort(mergeSort, create_random())
print ("Heap Sort")
print ("---------")
print_sort(heapSort, create_random())
print ("Quick Sort")
print ("----------")
print_sort(quickSort, create_random())
print ("Quick3 Sort")
print ("-----------")
print_sort(quick3Sort, create_random())