简体   繁体   中英

Worst case time complexity lists

I know that the best, average and worst case time complexities of binary search are at Best O(1); Average O(log n); Worst O(log n); for an array implementation. Likewise, I know that the best, average and worst case time complexities of insertion sort are at Best O(n); Average O(n^2); Worst O(n^2); for an array implementation.

However, how would I work out the time complexities of say binary search and insertion sort of singly linked lists; doubly linked lists; and circular linked lists implementations?

A brief answer would be to look at the computer steps done by your algorithm. You might want to look at at Algorithm Proofs, and BigO, Omega, and Theta notations. Different algorithms have different worst case and best case scenarios. Given a function F(n) with n given inputs. It has G(n) computer steps. Lets say G(n) = 5x^2 + 3x + 1. Most people usually look at Big-O. For Big(O) you drop all the coefficient and just take the leading Term. SO your Big-O would be x^2

You simply can't implement binary search with a linked list (neither single linked nor any of the others). It requires O(1) access to any index, that's simple with an array, but impossible with a list. Think of how you would pass from element n/2 to n/4 for eg without traversing all the elements in the middle, or from the beginning.

Insertion sort on the other hand is based on traversing the elements consecutively, which is not a problem in a linked list, and therefore will retain the same complexity. Note that the O(N^2) is still the case even if you have to walk over the entire list from the beginning for each element (in case of a single linked list), instead of swapping your way backwards as you would with an array. It does however require to modify the algorithm, which shows you a very fundamental lesson - the specific algorithm dictates the data structure, using a different data structure will usually require a change in the algorithm, and in some cases - also the complexity.

Whether you can use binary search or not depends on whether you can access items randomly or not(ie access item by index). You can never access item from list( singly linked lists; doubly linked lists; and circular linked lists)by index, so you can't implement binary search on list. However, you can use Skip List instead. You can get O(1)(best case) O(lnN)(average and worst) search in Skip List.

Since, you don't need access by index in insertion sort, there will be no difference between array and list for insertion sort. Typically, you need access the previous item to insert the current item, however, you can also order the items by reversed order and find the position from the beginning(ie head of the list) and reverse the list at last. It won't change the time complexity.

BinarySearch : require random access to the elements , can you get it in liked lists ? NO . (though you can use extra space to convert it into array , but that beats the fact you have to use linked lists only)

Insertion sort :
you need to get access the elements before a particular index , surely you can't do it in O(1) for singly linked lists .
you can traverse the list from beginning for each element, but that will make time complexity very high .

In doubly linked list , you can apply insertion sort easily as you can access previous element in O(1) time.

On above discussion you can easily think about circular lists also.

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM