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

前端 未结 4 609
爱一瞬间的悲伤
爱一瞬间的悲伤 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:06

    I think the running time is O(nlgn).. binary search takes lgn time.. and moving the elements takes O(n/2) in the worst case, which is "asymptotically the same" as O(n) [I can't find the 'belongs to' symbol.. :D]

    And I don't think the pseudo-code that you gave is insertion sort with binary search.

    0 讨论(0)
  • 2021-01-15 01:09

    Basically, the time needed for an insertion sort is composed of three factors.

    1. Going over every element in the SOURCE array
    2. Finding the right location to insert it into the DESTINATION array
    3. Doing the insert.

    What you are talking about concerns step 2. A naive way would be to go through the full DESTINATION array to find the insert location, which takes O(n). However, you can do a binary search which would only take you O(log(n)).

    You still need to do the insert, but the cost of that is dependent on the data structure. If you use a linked list, it will always take you a constant amount of time. If you use a simple Array, you can just do a memcpy() which should scale equally well. The approach you use in pseudo-code is a very naive approach and would never be valid in a real implementation. See http://www.docjar.com/html/api/java/util/ArrayList.java.html#421 for a real-life example of an INSERT for Arrays. System.arraycopy() is O(1).

    0 讨论(0)
  • 2021-01-15 01:24

    insertion sort pushes the elements of the array in order to free a space to the next element in line.
    so if you find where to enter the new element using binary search, you will still need to push all the elements after that index one step forward (to the right).
    so given an array sorted backwards: 10,9,8,7,6,5,4,3,2,1
    you will need to make i-1 pushes to the right in order to insert the i'th element (even if you use binary search) - worst case time: O(n^2)
    if you could insert the elements, one by one, to a list you wouldn't have to push the elements, but you will have to "pay" for the search of the proper location in the list (so in this implementation W.C.T is O(n^2)).

    a solution to this problem will be with some kind of synergy between lists and arrays so you could reach the i'th element in O(1) time (as in arrays) and could push a new element to a given location (say after the index j) in O(1) time (as in lists) - if you succeed, I believe you will win eternal glory!

    0 讨论(0)
  • 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...

    0 讨论(0)
提交回复
热议问题