Insertion Sort
Code
python.py
java-script.js
type-script.ts
def insertion_sort(arr):
# save array's length
N = len(arr)
for i in range(1, N):
# assign a pointer to the start of the loop
j = i
# loop while the pointer is > 0 and the previous item is greater than it
while j > 0 and arr[j] < arr[j - 1]:
# swap the pointer item with it's left item
arr[j], arr[j-1] = arr[j-1], arr[j]
# decrease the pointer
j -= 1
Pseudocode
pseudocode
insertionSort(array of numbers)
for i = 1 to i = length(array) - 1
j = i
while j > 0 and array[j] < array[j - 1]
swap array[j] and array[j-1]
j = j - 1