Completed Ugly Sorting Implementation