-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort functions.py
More file actions
66 lines (61 loc) · 1.63 KB
/
sort functions.py
File metadata and controls
66 lines (61 loc) · 1.63 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
def bubble_sort(L):
# O(n^2)
swap = False
while not swap:
print('bubble sort: ' + str(L))
swap = True
for j in range(1, len(L)):
if L[j-1] > L[j]:
swap = False
temp = L[j]
L[j] = L[j-1]
L[j-1] = temp
def selection_sort(L):
# O(n^2)
"""
find minimum and put it first, than look only from
the n-1 rest of the list and do the same.
afer i step the i first elements are smallest and sorted.
"""
suffixSt = 0
while suffixSt != len(L):
for i in range(suffixSt, len(L)):
if L[i] < L[suffixSt]:
L[suffixSt], L[i] = L[i], L[suffixSt]
suffixSt += 1
def merge(left, right):
"""
split list to two list. sort each and merge them.
"""
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
while (i < len(left)):
result.append(left[i])
i += 1
while (j < len(right)):
result.append(right[j])
j += 1
print('merge: ' + str(left) + '&' + str(right))
return result
def merge_sort(L):
print('merge sort: ' + str(L))
if len(L) < 2:
return L[:]
else:
mid = len(L)//2
left = merge_sort(L[:mid])
right = merge_sort(L[mid:])
return merge(left, right)
if __name__ == "__main__":
L = [5,4,38,2,85,25,4,6,2,8,2]
print ('original list: ' + str(L))
merge_sort(L)
# bubble_sort(L)
# print(L)