-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimised_insertionsort.py
More file actions
36 lines (29 loc) · 1008 Bytes
/
optimised_insertionsort.py
File metadata and controls
36 lines (29 loc) · 1008 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(lst):
for i in range(1, len(lst)):
target = lst[i]
j = i - 1
while j >= 0 and lst[j] > target:
lst[j + 1] = lst[j]
j = j - 1
lst[j + 1] = target
return lst
int_list = [3,2,1]
print(insertion_sort(int_list))
# PROCEDURE InsertionSort(BYREF Array : ARRAY)
# // Sort an array of integers
# DECLARE i : INTEGER // Unsorted loop index
# DECLARE j : INTEGER // Sorted loop index
# Declare Target : INTEGER // target to be inserted
# // Assume first element is sorted
# FOR i <- 2 TO Array.LENGTH // iterate unsorted elements
# Target <- Array[i]
# j <- i – 1
# // Perform insertion by shifting up elements which
# // are greater than target
# WHILE j > 0 AND Array[j] > Target
# Array[j + 1] <- Array[j]
# j <- j - 1
# ENDWHILE
# Array[j + 1] <- Target
# ENDFOR
# ENDPROCEDURE