functional implementation of insertion sort