Home:ALL Converter>Incorrect Worst-case time complexity of Insertion Sort

Incorrect Worst-case time complexity of Insertion Sort

Ask Time:2017-10-28T01:15:15         Author:Shivanand THANGALAPALLI

Json Formatter

Hi I am new to algorithms and am quite fascinated by it.

I am trying to figure out worst case time complexity of insertion sort and it is mentioned as O(n**2). Instead, we can have the time complexity as O(N*logN).

Here is my explanation,

THe insertion sort looks at the 1st element and assumes it is sorted. Next it looks at the 2nd element and compares with the predecessor sorted sublist of 1 element and inserts it based on the comparison with elements in the predecessor sorted sublist. This process is repeated similarly.

Everywhere it is mentioned that to insert an element into the predecessor sorted sublist, basically linear search,it takes O(N) time and as we have do these operations for n elements it takes us O(N**2).

However, if we use binary insertion to insert the element into predecessor sublist it should take O(logn) time where n is the length of sublist. Basically compare the new element with the middle element of predecessor sorted sublist and if it is greater than the middle element then new element lies between the middle element and the last element of the sublist.

As we repeat the operations for n items it should take us O(N*logN). We can use binary search approach as we know the predecessor sublist is sorted.

So shouldn't the worst case time complexity be O(N*logN) instead of O(N**2).

Author:Shivanand THANGALAPALLI,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46980328/incorrect-worst-case-time-complexity-of-insertion-sort
yy