-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
83 lines (62 loc) · 1.65 KB
/
sorting.py
File metadata and controls
83 lines (62 loc) · 1.65 KB
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
# imports
import random
import timeit
# vars
list = []
# funcs
def fillList(list, n):
for i in range(n+1):
list.append(i)
#print("The List: {0}\n".format(list))
def shuffle(list):
# shuffling given list
for _ in range(len(list)):
# indexes at which to switch numbers in list
switch = random.randint(0, len(list)-1)
switch2 = random.randint(0, len(list)-1)
if switch2 == switch:
switch2 = random.randint(0, len(list)-1)
# switching to numbers
temp = list[switch]
list[switch] = list[switch2]
list[switch2] = temp
print("The List after shuffling: {0}\n".format(list))
def reverseList(list):
# reversing a list
print("The List after reversing: {0}\n".format(list))
# sorting algorithms
def bubbleSort(list):
counter = 1
# sorting given list by comparing two elements each time. time = O(n²)
for i in range(0, len(list)-counter):
counter += 1
for j in range(0, len(list)-1):
if list[j] > list[j+1]:
temp = list[j+1]
list[j+1] = list[j]
list[j] = temp
print("The List after sorting via bubblesort: {0}\n".format(list))
def selectionSort(list):
# right now more like a bubblesort i think
for j in range(len(list)):
for i in range(len(list)-1):
if list[i] > list[i+1]:
lowestIndx = i+1
else:
lowestIndx = i
temp = list[i]
list[i] = list[lowestIndx]
list[lowestIndx] = temp
print("The List after sorting via selection sort: {0}\n".format(list))
# main-func
def main():
global list
fillList(list, 100)
#reverseList(list)
shuffle(list)
#bubbleSort(list)
selectionSort(list)
# calling main-func
main()
time = timeit.timeit(main, number=1)
print(f'Time taken: {time*1e2} milliseconds')