-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorting.py
57 lines (37 loc) · 1.16 KB
/
Sorting.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
def selection_sort(arr):
min_idx = 0
for i in range(0,len(arr)):
swaped = True
print("Top loop",i)
min_idx = i
for j in range(i+1,len(arr)-1):
print("Inner loop",j)
if arr[j]<arr[min_idx]:
swaped = False
min_idx = j
arr[i],arr[min_idx] = arr[min_idx],arr[i]
if swaped:
break
print(arr)
def bubble_sort(arr):
for i in range(0,len(arr)):
print("Top loop",i)
for j in range(0,len(arr)-1):
print("Inner loop",j)
if arr[j]>arr[j+1]:
arr[j+1],arr[j] = arr[j],arr[j+1]
print(arr)
def optimized_bubble_sort(arr):
for i in range(0,len(arr)):
print("Top loop",i)
swaped = True
for j in range(0,len(arr)-1):
print("Inner loop",j)
if arr[j]>arr[j+1]:
arr[j+1],arr[j] = arr[j],arr[j+1]
swaped = False
if swaped:
break
print(arr)
unsorted_array = [9,8,7,6,5,4,1,2,3]
selection_sort(unsorted_array)