forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
37 lines (34 loc) · 856 Bytes
/
insertion_sort.py
File metadata and controls
37 lines (34 loc) · 856 Bytes
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
#---------------------------------------
# Insertion Sort
#---------------------------------------
# not optimized, equiv to while version below, but uses for loop
def insertion_sort1(A):
for i in range(1, len(A)):
for j in range(i-1, -1, -1):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
else:
break
# not optimized, equiv to break version, but uses while loop
def insertion_sort2(A):
for i in range(1, len(A)):
j = i-1
while A[j] > A[j+1] and j >= 0:
A[j], A[j+1] = A[j+1], A[j]
j -= 1
# optimized - shifts instead of swapping
def insertion_sort3(A):
for i in range(1, len(A)):
curNum = A[i]
k = 0
for j in range(i-1, -2, -1):
k = j
if A[j] > curNum:
A[j+1] = A[j]
else:
break
A[k+1] = curNum
A = [5,9,1,2,4,8,6,3,7]
print(A)
insertion_sort1(A)
print(A)