Insertion
Last updated
Last updated
Insertion Sort is also another simple sorting algorithm that works by iteratively inserting each element of an array into its correct position within a sorted subarray.
Here are the basic steps of the Insertion Sort algorithm:
Starting with the second element in the array, iterate through the unsorted portion of the array.
For each element, compare it to the elements in the sorted portion of the array and insert it into the correct position.
Repeat step 2 for all remaining elements in the unsorted portion of the array until the entire array is sorted.
Insertion Sort has a time complexity of O(n^2), making it relatively slow for large datasets.
However, it is efficient for sorting small datasets and is often used as a building block for more complex sorting algorithms.
Additionally, Insertion Sort has a relatively low space complexity, making it useful in situations where memory usage is a concern.
Output