Skip to content

Commit cacb312

Browse files
committed
Preparing all of the algorithms for comparison test
1 parent f1f8a81 commit cacb312

10 files changed

+230
-49
lines changed

algorithms/Comparison.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from algorithms.bubbleSort import bubble_sort
2+
from algorithms.bucketSort import bucket_sort
3+
from algorithms.combSort import comb_sort
4+
from algorithms.countingSort import counting_sort
5+
from algorithms.heapSort import heap_sort
6+
from algorithms.insertionSort import insertion_sort
7+
from algorithms.LSDradixSort import lsd_radix_sort
8+
from algorithms.mergeSort import merge_sort
9+
from algorithms.pigeonholeSort import pigeonhole_sort
10+
from algorithms.quickSort import quick_sort
11+
from algorithms.selectionSort import selection_sort
12+
13+
from random import randint
14+
15+
how_many = 1000
16+
lower_integer = 0
17+
upper_integer = 99
18+
19+
if __name__ == "__main__":
20+
array = []
21+
for i in range(how_many):
22+
array.append(randint(lower_integer, upper_integer))

algorithms/LSDradixSort.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
from random import randint
22

3-
def counting_sort(arr, exp):
3+
4+
def counting_sort(arr, exp, comps, accesses):
45
n = len(arr)
56
newArr = [0] * n
67
count = [0] * 10
78
for i in range(n):
8-
index = arr[i]/exp
9+
accesses += 1
10+
index = arr[i]//exp
911
count[index % 10] += 1
10-
for i in range(1,10):
12+
for i in range(1, 10):
1113
count[i] += count[i-1]
1214
i = n - 1
1315
while i >= 0:
14-
index = arr[i]/exp
16+
accesses += 1
17+
index = arr[i]//exp
1518
newArr[count[index % 10] - 1] = arr[i]
1619
count[index % 10] -= 1
1720
i -= 1
1821
for i in range(len(arr)):
22+
accesses += 1
1923
arr[i] = newArr[i]
20-
return arr
24+
return (comps, accesses)
2125

2226

2327
def lsd_radix_sort(arr):
28+
comparisons = len(arr) - 1
29+
array_accesses = len(arr)
2430
maximum = max(arr)
2531
exp = 1
26-
while maximum/exp > 0:
27-
counting_sort(arr, exp)
32+
while maximum/exp >= 1:
33+
(comparisons, array_accesses) = counting_sort(
34+
arr, exp, comparisons, array_accesses)
2835
exp *= 10
29-
return arr
36+
print("LSD Radix sort:")
37+
print("No. comparisons: " + str(comparisons) +
38+
", no. array accesses: " + str(array_accesses))
3039

3140

3241
if __name__ == "__main__":
3342
array = []
3443
for i in range(20):
35-
array.append(randint(0,10))
36-
print(lsd_radix_sort(array))
44+
array.append(randint(0, 10))
45+
lsd_radix_sort(array)

algorithms/bubbleSort.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
from random import random
22

3+
34
def bubble_sort(arr):
5+
comparisons = 0
6+
array_accesses = 0
47
for i in range(len(arr)):
58
swapped = False
69
for j in range(len(arr) - i - 1):
10+
comparisons += 1
11+
array_accesses += 2
712
if arr[j] > arr[j + 1]:
13+
array_accesses += 2
814
arr[j + 1], arr[j] = arr[j], arr[j + 1]
915
swapped = True
1016
if not swapped:
1117
break
12-
return arr
18+
print("Bubble sort:")
19+
print("No. comparisons: " + str(comparisons) +
20+
", no. array accesses: " + str(array_accesses))
21+
1322

1423
if __name__ == "__main__":
1524
array = []
1625
for i in range(20):
1726
array.append(random())
1827
bubble_sort(array)
19-
print(bubble_sort(array))

algorithms/bucketSort.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
from random import randint
2-
from insertionSort import insertion_sort
2+
3+
4+
def insertion_sort(arr, comps, accesses):
5+
for i in range(1, len(arr)):
6+
accesses += 1
7+
chosenElem = arr[i]
8+
j = i - 1
9+
comps += 1
10+
accesses += 1
11+
while j >= 0 and arr[j] > chosenElem:
12+
comps += 1
13+
accesses += 1
14+
arr[j+1] = arr[j]
15+
j -= 1
16+
accesses += 1
17+
arr[j+1] = chosenElem
18+
return (comps, accesses)
19+
320

421
def bucket_sort(arr, k=5):
22+
comparisons = 0
23+
array_accesses = 0
24+
525
buckets = []
626
for i in range(k):
727
buckets.append([])
828
newTab = []
929
maximum = max(arr)
1030
for i in range(len(arr)):
31+
array_accesses += 1
1132
index = int(k * arr[i] / maximum)
1233
if index == k:
1334
index -= 1
1435
buckets[index].append(arr[i])
1536
for i in range(k):
16-
buckets[i] = insertion_sort(buckets[i])
37+
(comparisons, array_accesses) = insertion_sort(
38+
buckets[i], comparisons, array_accesses)
1739
for j in range(len(buckets[i])):
1840
newTab.append(buckets[i][j])
19-
return newTab
41+
print("Bucket sort:")
42+
print("No. comparisons: " + str(comparisons) +
43+
", no. array accesses: " + str(array_accesses))
2044

2145

2246
if __name__ == "__main__":
2347
array = []
2448
for i in range(20):
2549
array.append(randint(0, 9))
26-
print(bucket_sort(array))
50+
bucket_sort(array)

algorithms/combSort.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from random import random
22

3+
34
def comb_sort(arr):
5+
comparisons = 0
6+
array_accesses = 0
47
factor = 1.3
58
gap = len(arr)
69
isSorted = False
@@ -11,14 +14,19 @@ def comb_sort(arr):
1114
factor = 1
1215
for i in range(len(arr) - gap):
1316
offsetIndex = gap + i
17+
array_accesses += 2
18+
comparisons += 1
1419
if arr[i] > arr[offsetIndex]:
20+
array_accesses += 2
1521
arr[i], arr[offsetIndex] = arr[offsetIndex], arr[i]
1622
isSorted = False
17-
return arr
23+
print("Comb sort:")
24+
print("No. comparisons: " + str(comparisons) +
25+
", no. array accesses: " + str(array_accesses))
1826

1927

2028
if __name__ == "__main__":
2129
array = []
2230
for i in range(20):
2331
array.append(random())
24-
print(comb_sort(array))
32+
comb_sort(array)

algorithms/countingSort.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
from random import randint
22

3+
34
def counting_sort(arr):
5+
comparisons = 2 * len(arr)
6+
array_accesses = 2 * len(arr)
47
spread = max(arr) - min(arr) + 1
58
countArr = [0] * spread
69
newArr = [0] * len(arr)
710
for i in range(len(arr)):
11+
array_accesses += 2
812
countArr[arr[i]] += 1
913
for i in range(1, spread):
14+
array_accesses += 2
1015
countArr[i] += countArr[i-1]
1116
for i in range(len(arr)-1, -1, -1):
17+
array_accesses += 4
1218
countArr[arr[i]] -= 1
1319
newArr[countArr[arr[i]]] = arr[i]
14-
return newArr
20+
print("Counting sort:")
21+
print("No. comparisons: " + str(comparisons) +
22+
", no. array accesses: " + str(array_accesses))
1523

1624

1725
if __name__ == "__main__":
1826
array = []
1927
for i in range(20):
20-
array.append(randint(0,10))
21-
print(counting_sort(array))
28+
array.append(randint(0, 10))
29+
counting_sort(array)

algorithms/heapSort.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from random import random
2+
3+
4+
def heapify(arr, n, i, comps, accesses):
5+
largest = i
6+
l = 2*i + 1
7+
r = 2*i + 2
8+
9+
comps += 1
10+
accesses += 2
11+
if l < n and arr[i] < arr[l]:
12+
largest = l
13+
14+
comps += 1
15+
accesses += 2
16+
if r < n and arr[largest] < arr[r]:
17+
largest = r
18+
19+
if largest != i:
20+
accesses += 4
21+
arr[i], arr[largest] = arr[largest], arr[i]
22+
(comps, accesses) = heapify(arr, n, largest, comps, accesses)
23+
24+
return (comps, accesses)
25+
26+
27+
def heap_sort(arr):
28+
comparisons = 0
29+
array_accesses = 0
30+
31+
n = len(arr)
32+
for i in range(n//2 - 1, -1, -1):
33+
(comparisons, array_accesses) = heapify(
34+
arr, n, i, comparisons, array_accesses)
35+
36+
for i in range(n-1, 0, -1):
37+
array_accesses += 4
38+
arr[i], arr[0] = arr[0], arr[i]
39+
(comparisons, array_accesses) = heapify(
40+
arr, i, 0, comparisons, array_accesses)
41+
42+
print("Heap sort:")
43+
print("No. comparisons: " + str(comparisons) +
44+
", no. array accesses: " + str(array_accesses))
45+
46+
47+
if __name__ == "__main__":
48+
array = []
49+
for i in range(20):
50+
array.append(random())
51+
heap_sort(array)

algorithms/insertionSort.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
from random import random
22

3+
34
def insertion_sort(arr):
5+
comparisons = 0
6+
array_accesses = 0
47
for i in range(1, len(arr)):
8+
array_accesses += 1
59
chosenElem = arr[i]
610
j = i - 1
11+
array_accesses += 1
12+
comparisons += 1
713
while j >= 0 and arr[j] > chosenElem:
14+
array_accesses += 2
815
arr[j+1] = arr[j]
916
j -= 1
17+
array_accesses += 1
1018
arr[j+1] = chosenElem
11-
return arr
19+
print("Insertion sort:")
20+
print("No. comparisons: " + str(comparisons) +
21+
", no. array accesses: " + str(array_accesses))
1222

1323

1424
if __name__ == "__main__":
1525
array = []
1626
for i in range(20):
1727
array.append(random())
18-
print(insertion_sort(array))
28+
insertion_sort(array)

algorithms/mergeSort.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
11
from random import random
22

3-
def merge_sort(arr):
4-
if len(arr) >1:
3+
4+
def merge_sort(arr):
5+
comparisons = 0
6+
array_accesses = 0
7+
if len(arr) > 1:
8+
array_accesses += len(arr)
59
mid = len(arr)//2
610
L = arr[:mid]
711
R = arr[mid:]
8-
9-
merge_sort(L)
10-
merge_sort(R)
11-
12+
13+
(comparisons, array_accesses) = merge_sort(L)
14+
(comparisons, array_accesses) = merge_sort(R)
15+
1216
i = j = k = 0
13-
14-
while i < len(L) and j < len(R):
15-
if L[i] < R[j]:
16-
arr[k] = L[i]
17-
i+= 1
18-
else:
19-
arr[k] = R[j]
20-
j+= 1
21-
k+= 1
22-
23-
while i < len(L):
24-
arr[k] = L[i]
25-
i+= 1
26-
k+= 1
27-
28-
while j < len(R):
29-
arr[k] = R[j]
30-
j+= 1
31-
k+= 1
3217

18+
while i < len(L) and j < len(R):
19+
comparisons += 1
20+
array_accesses += 4
21+
if L[i] < R[j]:
22+
arr[k] = L[i]
23+
i += 1
24+
else:
25+
arr[k] = R[j]
26+
j += 1
27+
k += 1
28+
29+
while i < len(L):
30+
array_accesses += 2
31+
arr[k] = L[i]
32+
i += 1
33+
k += 1
34+
35+
while j < len(R):
36+
array_accesses += 2
37+
arr[k] = R[j]
38+
j += 1
39+
k += 1
40+
41+
return (comparisons, array_accesses)
42+
43+
44+
print("Merge sort:")
45+
print("No. comparisons: " + str(comparisons) +
46+
", no. array accesses: " + str(array_accesses))
3347

3448
if __name__ == "__main__":
3549
array = []
3650
for i in range(20):
3751
array.append(random())
3852
merge_sort(array)
39-
print(array)

0 commit comments

Comments
 (0)