-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorts.py
133 lines (100 loc) · 3.33 KB
/
Sorts.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
"""
This module contains sorting algorithms.
Student Name: Michael Fourie
Student Number: 20102203
"""
def swap(aList, x, y):
"""Exchanges the values of a[x] and a[y]"""
aList[x], aList[y] = aList[y], aList[x]
def bubbleSort(aList):
"""
Bubble sort algorithm.
Input: a list of unsorted values
Output: a count of sorts made
"""
swapped = True
# Holds count of sorts made
count = 0
while swapped:
swapped = False
for i in range(1, len(aList)):
if aList[i - 1] > aList[i]:
swap(aList, i - 1, i)
swapped = True
count += 1
# Returns count of sorts made
return count
def bubbleSortOpt(aList):
"""
Sorts list a into ascending order by value.
This version avoids unnecessary passes over already sorted elements at the end of the list.
Input: a list of unsorted values
Output: a count of sorts made
"""
# Holds count of comparisons
count = 0
n = len(aList)
swapped = True
while swapped:
swapped = False
for i in range(1, n):
if aList[i - 1] > aList[i]:
swap(aList, i - 1, i)
swapped = True
count += 1 # for analysis only
n -= 1
# Returns count of sorts made
return count
def selectionSort(aList):
"""
Sorts list a in ascending order by value.
Input: list of unsorted values
Output: count of sorts made
"""
n = len(aList)
# Holds count of swaps
count = 0
for i in range(n - 1):
min = i;
for j in range(i + 1, n):
if (aList[j] < aList[min]):
min = j;
# Add value to count variable
count += 1
if (min != i):
swap(aList, i, min)
return count
def insertionSort(aList):
"""
This sorts a list in ascending order by values
Input: list of unsorted values
Output: count of sorts made
"""
# Count number of sorts
count = 0
for i in range(1, len(aList)):
# use j to itterate through the list from position i towards position 0
j = i
while (j > 0) and (aList[j - 1] > aList[j]):
# if the element on the left is larger, swap the items
aList[j - 1], aList[j] = aList[j], aList[j - 1]
# Add value to count variable
count += 1
j = j - 1
return count
if __name__ == '__main__':
aList = [5, 2, 13, 425, 34, 76, 975, 34576, 46, 64, 245, 7, 13]
print(aList, 'list unsorted')
print(bubbleSort(aList), 'sorts made to sort the list.\nSorted list: ', aList)
print('')
aList = [5, 2, 13, 425, 34, 76, 975, 34576, 46, 64, 245, 7, 13]
print(aList, 'list unsorted')
print(bubbleSort(aList), 'sorts made to sort the list.\nSorted list: ', aList)
print('')
aList = [5, 2, 13, 425, 34, 76, 975, 34576, 46, 64, 245, 7, 13]
print(aList, 'list unsorted')
print(selectionSort(aList), 'sorts made to sort the list.\nSorted list: ', aList)
print('')
aList = [5, 2, 13, 425, 34, 76, 975, 34576, 46, 64, 245, 7, 13]
print(aList, 'list unsorted')
print(insertionSort(aList), 'sorts made to sort the list.\nSorted list: ', aList)