Insertion Sort - Part 1

  • + 11 comments

    @ilmash it's a great problem as it explains the essence of insertion sort.
    In insertion sort you assume the array upto some i is sorted and then insert the ith element.

    Here's from wikipedia

    for i = 1 to length(A) - 1
        x = A[i]
        j = i
        while j > 0 and A[j-1] > x
            A[j] = A[j-1]
            j = j - 1
        A[j] = x
    

    In this challenge you are only performing the final iteration of i = n-1

    Given a sorted list with an unsorted number V in the right-most cell, can you write some simple code to insert V into the array so it remains sorted?

    And by printing each time you ensure that you have understood the swap properly :)

    Good luck