Improving worst-case running time of insertion sort using binary search

前端 未结 4 694
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 00:34

The while loop uses linear search to scan backwards. However, we know that the array within the while loop is already sorted. So we can replace the linear search with binary

4条回答
  •  耶瑟儿~
    2021-01-15 01:32

    Binary searching for the insertion point can provide a slight improvement, mainly by removing the key comparison from the loop that shifts elements up. The difference won't be significant until you are sorting a large amount of data. Of course, if you have a large dataset you should be using a better sorting algorithm...

提交回复
热议问题