➤Insertion sort is a simple sorting algorithm. It starts with a partially sorted array and
iteratively inserts each element into its correct position, shifting the larger elements to the right. This process continues until the entire array is sorted.
How Insertion-sort works?
➤ We have an unsorted array arr = [ 1, 4, 2, 5, -2, 3 ], In insertion sort, elements are compared by starting from the
second element and comparing it with each preceding element in the sorted portion. If the element is smaller, it is shifted to the right until the
correct position is found. This process continues for each unsorted element, resulting in a sorted array.
How the element compare?
Demonstration of insertion Sort
Demonstration of insertion Sort
➤Optimized Insertion Sort: Iterate through the array,
shifting elements to the right until the correct position is found for the current element. This reduces
the number of write operations. Time complexity is still O(n^2) in the worst case. Use more efficient sorting algorithms for larger data sets.