Modified insertion sort to reduce cost