Saturday, January 3, 2015

Insertion sort

For i = 1 to N
      For j = i until j is 0 or array[j] > array[j-1]; j--
            Swap j with j - 1

The array is already sorted from 0 to i - 1, so for the ith element we need to keep swapping it to the left until it's greater than its neighbor.

No comments:

Post a Comment

There was an error in this gadget