Insertion Sort is a simple and intuitive sorting algorithm that works by building a sorted list one element at a time. It takes each element from the unsorted portion and inserts it into the correct position in the sorted portion.
👁 Insertion-sorting Insertion Sort How Insertion Sort Works Start from the second element (as the first one is already considered sorted). Compare the current element (called key) with the elements before it. Shift all larger elements one position to the right. Insert the key into its correct position. Repeat until the entire array is sorted. Python Implementation Output [-1, 5, 11, 12, 13]
Explanation:
for i in range(1, n) : Iterates through the array from the second element onward. key = arr[i] : Stores the current element to be inserted in the sorted portion. Inner while loop : Shifts elements greater than key one position to the right. arr[j + 1] = key : Inserts key into its correct sorted position. Related Articles: