#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort
#

class Array {
    method insertion_sort {
        { |i|
            var j = i;
            var k = self[i+1];
            while ((j >= 0) && (k < self[j])) {
                self[j+1] = self[j];
                j--;
            }
            self[j+1] = k;
        } * self.end;
        return self;
    }
}
 
var numbers = [7,6,5,9,8,4,3,1,2,0];
say numbers.insertion_sort;
 
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say insertion_sort(strs);

assert_eq(strs, sort(strs))
