Insertion sort worst case

核能气质少年 提交于 2020-01-17 05:18:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!