-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57_sort_algorithms.py
More file actions
53 lines (47 loc) · 1.22 KB
/
57_sort_algorithms.py
File metadata and controls
53 lines (47 loc) · 1.22 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
def bubble_sort(arr) :
iters = len(arr)
for i in range(iters-1,0,-1) :
for j in range(i) :
if arr[j] > arr[j+1] :
tmp = arr[j+1]
arr[j+1] = arr[j]
arr[j] = tmp
print(arr)
def selection_sort(arr) :
iters = len(arr)
for i in range(iters-1) :
min_idx = i
for j in range(i+1,iters) :
if arr[j] < arr[min_idx] :
min_idx = j
tmp = arr[i]
arr[i] = arr[min_idx]
arr[min_idx] = tmp
print(arr)
def insertion_sort(arr) :
iters = len(arr)
for i in range(1,iters) :
j = i-1
tmp = arr[i]
while j>=0 and arr[j] > tmp :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = tmp
print(arr)
def counting_sort(arr) :
iters = len(arr)
max_val = max(arr)
cnt = [0]*(max_val+1)
for i in range(iters) :
cnt[arr[i]] += 1
idx=0
for i in range(max_val+1) :
while cnt[i] > 0 :
arr[idx] = i
idx+=1
cnt[i] -= 1
print(arr)
bubble_sort([4,5,1,3,4,2,3,1,4,5,2,3])
selection_sort([4,5,1,3,4,2,3,1,4,5,2,3])
insertion_sort([4,5,1,3,4,2,3,1,4,5,2,3])
counting_sort([4,5,1,3,4,2,3,1,4,5,2,3])