forked from OliverIgnetik/Data-Structures-Algorithms-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.py
More file actions
39 lines (31 loc) · 942 Bytes
/
Copy pathinsertionSort.py
File metadata and controls
39 lines (31 loc) · 942 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
38
39
def insertionsort(arr):
length = len(arr)
i = 1
end = arr[0]
while i < length:
if arr[i] < end:
x = arr.pop(i)
for j in range(0, i):
if x < arr[j]:
arr.insert(j, x)
break
end = arr[i]
i += 1
return arr
def insertion_sort(arr):
# For every index in array
for i in range(1, len(arr)):
# Set current values and position
currentvalue = arr[i]
position = i
# this is a good solution if you don't care about writing costs
# Sorted Sublist
while position > 0 and arr[position-1] > currentvalue:
# shift items to the right if they are bigger
arr[position] = arr[position-1]
position -= 1
arr[position] = currentvalue
return arr
arr = [6, 5, 3, 1, 8, 7, 2, 4]
print(insertion_sort(arr))
# print(insertionsort(arr))