-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorts.py
119 lines (97 loc) · 3.5 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
import time
from random import randint
#from random import shuffle
# def bogoSort(numbers):
# srtNums = sorted(numbers)
# while numbers != srtNums:
# shuffle(numbers)
# return numbers
def selectionSort(numbers):
for i in range(len(numbers)):
index_min = i
for j in range(i, len(numbers)):
if numbers[j] < numbers[index_min]:
index_min = j
numbers[index_min], numbers[i] = numbers[i], numbers[index_min]
return numbers
def insertionSort(numbers):
for i in range(1, len(numbers)):
for j in range(i):
if numbers[j] > numbers[i]:
numbers[i], numbers[j] = numbers[j], numbers[i]
return numbers
def bubbleSort(numbers):
for i in range(len(numbers)-1):
for j in range(len(numbers) - 1 - i):
if numbers[j] > numbers[j + 1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
return numbers
def quickSort(numbers):
tabMore = []
tabEqual = []
tabLess = []
if len(numbers) > 1:
pivot = numbers[randint(0, len(numbers)-1)]
for i in range(len(numbers)):
if numbers[i] > pivot:
tabMore.append(numbers[i])
elif numbers[i] < pivot:
tabLess.append(numbers[i])
else:
tabEqual.append(pivot)
return quickSort(tabLess) + tabEqual + quickSort(tabMore)
else:
return numbers
def mergeSort(numbers):
if len(numbers) > 1:
numbers1 = numbers[:len(numbers)//2]
numbers2 = numbers[len(numbers)//2:]
numbers1 = mergeSort(numbers1)
numbers2 = mergeSort(numbers2)
k, i, j = 0, 0, 0
while i < len(numbers1) and j < len(numbers2):
if numbers1[i] < numbers2[j]:
numbers[k] = numbers1[i]
i += 1
else:
numbers[k] = numbers2[j]
j += 1
k += 1
return numbers
def heapSort(numbers):
def heapify(numbers, parentIndex):
largestValueIndex = parentIndex
firstChildIndex = 2 * parentIndex + 1
secondChildIndex = 2 * parentIndex + 2
if len(numbers) > firstChildIndex and numbers[largestValueIndex] < numbers[firstChildIndex]:
largestValueIndex = firstChildIndex
numbers[largestValueIndex], numbers[firstChildIndex] = numbers[firstChildIndex], numbers[largestValueIndex]
if len(numbers) > secondChildIndex and numbers[largestValueIndex] < numbers[secondChildIndex]:
largestValueIndex = secondChildIndex
numbers[largestValueIndex], numbers[secondChildIndex] = numbers[secondChildIndex], numbers[largestValueIndex]
return numbers
for i in range(len(numbers)//2 - 1, -1, -1):
numbers = heapify(numbers, i)
return numbers
def countingSort(numbers):
tab = [0] * (max(numbers) + 1)
for i in numbers:
tab[i] += 1
numbers = []
for i in range(len(tab)):
for j in range(tab[i]):
numbers.append(i)
return numbers
def main():
sorts = [selectionSort, insertionSort, bubbleSort, quickSort, mergeSort, heapSort, countingSort]
f = open("Values/selectionSort_values1000_32.txt")
tab = list(map(int, f.readline().strip().split(", ")))
print(tab)
for sort in sorts:
start = time.time()
print("{}: {}".format(sort.__name__, sort(tab)))
end = time.time()
t = end - start
print("Time: {}".format(str(t)))
if __name__ == '__main__':
main()