C based list sorting algorithm