Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1.35 KB

Insertion-Sort.md

File metadata and controls

36 lines (23 loc) · 1.35 KB

Insertion Sort

Inserion sort is an inplace sorting algorithm meaning it won't take any extra space to sort the array items. It works similar to the way you sort playing cards in your hand. Consider you are dealt 4 cards one after the other. You pick one and keep in your hand, after that for the next card (key) you compare with the one in your hand and place it accordingly and so on until all the cards are in your hand.

Steps

To sort an array of size n in ascending order:

  1. Iterate from second element, arr[1] to arr[n-1] over the array.
  2. Compare the current element (key) to its predecessor.
  3. If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Example

Given array is 12 11 13 5 6 7

Sorted array is 5 6 7 11 12 13

Implementation

Video URL

Youtube Video about Insertion Sort

Others

Wikipedia