-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.py
78 lines (62 loc) · 1.45 KB
/
sort.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
def merge_sort(arr):
if len(arr)<=1:
return
mid = len(arr)//2
left = arr[mid:]
right = arr[:mid]
merge_sort(left)
merge_sort(right)
i=j=k=0
while i < len(left) and j < len(right):
if left[i]<right[j]:
arr[k]=left[i]
i += 1
else:
arr[k]=right[j]
j += 1
k += 1
while i < len(left):
arr[k]=left[i]
k += 1
i += 1
while j < len(right):
arr[k]=right[j]
k += 1
j += 1
def selection_sort(arr):
n = len(arr)
for step in range(n):
min_idx = step
for i in range(step+1,n):
if arr[min_idx] > arr[i]:
min_idx = i
(arr[step],arr[min_idx])=(arr[min_idx],arr[step])
def insertion_sort(arr):
n = len(arr)
for step in range(1,n):
stepValue = arr[step]
j = step - 1
while j>=0 and arr[j] > stepValue:
arr[j+1]=arr[j]
j = j -1
arr[j+1]=stepValue
def bubble_sort(arr):
n = len(arr)
for step in range(n-1):
for j in range(n-1-step):
if arr[j]>arr[j+1]:
(arr[j],arr[j+1])=(arr[j+1],arr[j])
swapped=True
if not swapped:
return
def print_list(arr):
print(arr)
arr = [3,8,4,7,1,5]
#arr=[5]
#arr=[]
print_list(arr)
#selection_sort(arr)
#insertion_sort(arr)
#bubble_sort(arr)
merge_sort(arr)
print_list(arr)