Insertion Sort Visualization

What is Insertion Sort Algorithm?

Insertion Sort is a simple and efficient sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as QuickSort, heapsort, or merge sort, but it performs well for small datasets or lists that are already partially sorted.



def insertion_sort(arr):
  for i in range(1, len(arr)):
    key = arr[i]
    j = i - 1
    while j >= 0 and key < arr[j]:
      arr[j + 1] = arr[j]
      j -= 1
    arr[j + 1] = key