Insertion sort builds the final sorted array one element at a time. It takes each element from the unsorted part of the array and inserts it into its correct position within the already-sorted part. Think of how you sort playing cards in your hand: you pick up cards one at a time and insert each new card into its correct spot among the cards you’re already holding, shifting the others over as needed. At every step, the left portion of the array is always fully sorted. We take the next element, and slide it left past anything bigger than it, until it lands in the right spot.
Let’s sort the following array from smallest to largest:

We pick up 3 (highlighted in red) and compare it against the sorted region to its left.






This one needs to move all the way to the front.















✅ Final sorted array:

Enter up to 20 comma-separated numbers.
Insertion sort is not suitable for large random datasets/arrays. However, it performs rather well when the arrays provided are nearly-sorted (best-case scenario). In practice, it is usually used internally by some hybrid sorting algorithms for small sub-arrays.