Skip to content

Commit f25dfac

Browse files
committed
made Comparison.py which tests all of the included algorithms and provides a feedback on comparisons/array_accesses number
1 parent cacb312 commit f25dfac

7 files changed

+83
-28
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
algorithms/__pycache__
2+
*.pyc

algorithms/Comparison.py

+39-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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
1+
from bubbleSort import bubble_sort
2+
from bucketSort import bucket_sort
3+
from combSort import comb_sort
4+
from countingSort import counting_sort
5+
from heapSort import heap_sort
6+
from insertionSort import insertion_sort
7+
from LSDradixSort import lsd_radix_sort
8+
from mergeSort import merge_sort
9+
from pigeonholeSort import pigeonhole_sort
10+
from quickSort import quick_sort
11+
from selectionSort import selection_sort
1212

1313
from random import randint
1414

@@ -20,3 +20,31 @@
2020
array = []
2121
for i in range(how_many):
2222
array.append(randint(lower_integer, upper_integer))
23+
temp = array[:]
24+
bubble_sort(temp)
25+
temp = array[:]
26+
bucket_sort(temp)
27+
temp = array[:]
28+
comb_sort(temp)
29+
temp = array[:]
30+
counting_sort(temp)
31+
temp = array[:]
32+
heap_sort(temp)
33+
temp = array[:]
34+
insertion_sort(temp)
35+
temp = array[:]
36+
lsd_radix_sort(temp)
37+
temp = array[:]
38+
pigeonhole_sort(temp)
39+
temp = array[:]
40+
selection_sort(temp)
41+
temp = array[:]
42+
(comparisons, array_accesses) = merge_sort(temp)
43+
print("Merge sort:")
44+
print("No. comparisons: " + str(comparisons) +
45+
", no. array accesses: " + str(array_accesses))
46+
temp = array[:]
47+
(comparisons, array_accesses) = quick_sort(temp, 0, len(temp) - 1, 0, 0)
48+
print("Quick sort:")
49+
print("No. comparisons: " + str(comparisons) +
50+
", no. array accesses: " + str(array_accesses))

algorithms/insertionSort.pyc

-689 Bytes
Binary file not shown.

algorithms/mergeSort.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ def merge_sort(arr):
4141
return (comparisons, array_accesses)
4242

4343

44-
print("Merge sort:")
45-
print("No. comparisons: " + str(comparisons) +
46-
", no. array accesses: " + str(array_accesses))
47-
4844
if __name__ == "__main__":
4945
array = []
5046
for i in range(20):
5147
array.append(random())
52-
merge_sort(array)
48+
(comparisons, array_accesses) = merge_sort(array)
49+
print("Merge sort:")
50+
print("No. comparisons: " + str(comparisons) +
51+
", no. array accesses: " + str(array_accesses))

algorithms/pigeonholeSort.py

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

3+
34
def pigeonhole_sort(arr):
5+
comparisons = 2*len(arr)
6+
array_accesses = 2*len(arr)
47
minimum = min(arr)
58
size = max(arr) - minimum + 1
69
holes = [0] * size
710
for x in arr:
11+
array_accesses += 1
812
holes[x - minimum] += 1
913
i = 0
1014
for count in range(size):
1115
while holes[count] > 0:
1216
holes[count] -= 1
17+
array_accesses += 1
1318
arr[i] = count + minimum
1419
i += 1
15-
return arr
20+
print("Pigeonhole sort:")
21+
print("No. comparisons: " + str(comparisons) +
22+
", no. array accesses: " + str(array_accesses))
23+
1624

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

algorithms/quickSort.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
from random import random
22

33

4-
def partition(arr, bottom, top):
4+
def partition(arr, bottom, top, comps, accesses):
55
i = bottom - 1
66
pivot = arr[top]
77
for j in range(bottom, top):
8+
accesses += 1
9+
comps += 1
810
if arr[j] < pivot:
911
i += 1
12+
accesses += 3
1013
arr[i], arr[j] = arr[j], arr[i]
1114

15+
accesses += 4
1216
arr[i+1], arr[top] = arr[top], arr[i+1]
13-
return (i + 1)
17+
return (i + 1, comps, accesses)
1418

1519

16-
def quick_sort(arr, bottom, top):
20+
def quick_sort(arr, bottom, top, comps, accesses):
1721
if bottom < top:
18-
pi = partition(arr, bottom, top)
19-
quick_sort(arr, bottom, pi-1)
20-
quick_sort(arr, pi+1, top)
22+
(pi, comps2, accesses2) = partition(arr, bottom, top, comps, accesses)
23+
comps += comps2
24+
accesses += accesses2
25+
quick_sort(arr, bottom, pi-1, comps, accesses)
26+
quick_sort(arr, pi+1, top, comps, accesses)
27+
return (comps, accesses)
2128

2229

2330
if __name__ == "__main__":
2431
array = []
2532
for i in range(20):
2633
array.append(random())
27-
quick_sort(array, 0, len(array) - 1)
28-
print(array)
34+
(comparisons, array_accesses) = quick_sort(array, 0, len(array) - 1, 0, 0)
35+
print("Quick sort:")
36+
print("No. comparisons: " + str(comparisons) +
37+
", no. array accesses: " + str(array_accesses))

algorithms/selectionSort.py

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

3+
34
def selection_sort(arr):
5+
comparisons = 0
6+
array_accesses = 0
47
for i in range(len(arr)):
58
minimum = i
69
for j in range(i+1, len(arr)):
10+
array_accesses += 2
11+
comparisons += 1
712
if arr[j] < arr[minimum]:
813
minimum = j
914
if minimum != i:
15+
array_accesses += 4
1016
arr[i], arr[minimum] = arr[minimum], arr[i]
11-
return arr
17+
print("Selection sort:")
18+
print("No. comparisons: " + str(comparisons) +
19+
", no. array accesses: " + str(array_accesses))
20+
1221

1322
if __name__ == "__main__":
1423
array = []
1524
for i in range(20):
1625
array.append(random())
17-
print(selection_sort(array))
26+
selection_sort(array)

0 commit comments

Comments
 (0)