-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgos.py
executable file
·83 lines (72 loc) · 2.02 KB
/
algos.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
#!/usr/bin/python3
def comapareArrays(a,b):
if len(a) != len(b): return False
i=0;
while (i < len(a)):
if(a[i] != b[i]): return False
i+=1
return True
def selectionSort(ar):
m = len(ar)
last = 0
while last < m:
minn = last
for i in range(last, len(ar)):
if ar[i] < ar[minn]:
minn = i
b = ar[last]
a = ar[minn]
ar[last] = a
ar[minn] = b
last += 1
return ar
def insertionSort(ar):
k = ar[0]
for i in range(1, len(ar)):
j = i
t = ar[i]
k = t
while (ar[j-1] > k):
ar[j]=ar[j-1]
j-= 1
ar[j] = t
return ar
"""
geschafft?
"""
def quickSort(daten, links, rechts):
i = links
j = rechts
pivot = daten[j]
if i > j: return daten
if daten[i] > pivot:
di = daten[i]
daten[i] = pivot
daten[j] = di
daten = quickSort(daten, links+1, rechts)
daten = quickSort(daten, links, rechts-1)
return daten
def getDateTime():
from datetime import datetime
now = datetime.now()
dt_string = now.strftime("%d.%m.%Y %H:%M:%S")
return dt_string
def runTests():
print("Running ./algos.py\t", getDateTime())
expected = [0,10,30,40,200,227,600,4000]
ar = [227, 0,10,200,4000,30,40,600]
a = selectionSort(ar)
assert comapareArrays(a, expected), "[-] The output is different than expected for selectionSort"
print("[+] selectionSort Test passed")
expected = [10,30,40,200,400,4000]
ar1 = [10,400,40,30,4000,200]
b = insertionSort(ar1)
assert comapareArrays(b, expected), "[-] The output is different than expected for insertSort"
print("[+] insertSort Test passed")
expected = [10,30,40,200,400,4000]
ar1 = [10,400,40,30,4000,200]
b = quickSort(ar1, 0,len(ar1)-1)
assert comapareArrays(b, expected), "[-] The output is different than expected for quickSort"
print("[+] quickSort Test passed")
if __name__ == "__main__":
runTests()