问题
Why is the worst case for insertion sort n^2? Can it be nlogn with binary search? Minimize the location search to log(n) and then use a low level function like memmove to minimize swaps?
回答1:
Can it be nlogn with binary search?
You will need to sort the array first to use binary search, as binary search is only possible in sorted array
Edit Even if the first part of the array is sorted. The case now is that, you find the position to insert in logn steps. Then You actually need to insert the value there. For that you will need to move all the elements to right requiring n steps at worst case.
来源:https://stackoverflow.com/questions/38156136/insertion-sort-worst-case