-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.py
More file actions
32 lines (24 loc) · 862 Bytes
/
insertion_sort.py
File metadata and controls
32 lines (24 loc) · 862 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
def insertion_sort(array):
for index in range(1, len(array)):
temp_value = array[index]
position = index - 1
while position >= 0:
if array[position] > temp_value:
array[position + 1] = array[position]
position = position - 1
else:
break
array[position + 1] = temp_value
return array
#The second example is my version of the insertion sort algorithm
def insertion_sort(array):
for index in range(1, len(array)):
temp_value = array[index]
position = index - 1
while position >= 0 and array[position] > temp_value:
array[position + 1] = array[position]
position = position - 1
array[position + 1] = temp_value
return array
result = insertion_sort([12,11,85,102,1,7])
print(result)