views:

631

answers:

5

From wikipedia:

Sorted list implementation: Like a checkout line at the supermarket, but where important people get to "cut" in front of less important people. (O(n) insertion time, O(1) get-next time, O(n*log(n)) to build)

I think if searching the insert position with binary search algorithm,the insertion time complexity should be O(log(n)).Here I treat the arrival order of jobs as a factor of priority.

So am I wrong or wikipedia is incorrect?

Update: According to the strict definition of list from TAOCP:

A linear list is a sequence of n >=0 nodes X1, X[2], ... , X[n] whose essential structural properities involve only the relative positions between items as they appear in a line.

I assume the list wikipedia refer is not linked-list,and it could be array.

thanks.

+4  A: 

It seems that in your quote, Wikipedia is referring to a priority queue backed by a sorted list, rather than a heap. To insert an item into a sorted list requires O(n) time (assuming that we are maintaining its sortedness).

danben
I dont think so.The list should be sorted before the insertion. That's why the time complexity of build-up is O(n*log(n)) which is time complexity of general sort algorithm.
Jichao
@jcyang consider the list `1 -> 2 -> 3 -> 4` and try inserting element `5` into it by hand. Note that you don't have random access (there is no `list[3]`, there is only `list->next`)
laura
@laura:List is not linked-list.It just are typically implemented either as linked lists (either singly or doubly-linked) or as arrays.So if I implementate the prority tree as array,then I get random access.
Jichao
@jcyang - even if you are implementing your list as an array, your statements are incorrect. Insertion into an array is O(n) in the worst case and average case, because you need to shift ahead all elements in the array past the point of insertion.
danben
+2  A: 

Binary search is indeed O(log n) but binary search works on arrays - it works in this time because you can access any element in O(1).

However, in literature when you see the term list you should be thinking about linked lists. In a list therefore, you do not have O(1) access time, but rather you need to search for the position "by hand" - so inserting an element would take O(n).

laura
also if it were an array you can find the location in O(log(n)) but inserting will still be O(n) as you have to copy the array
jk
But array is a kind of list also.
Jichao
@jk:+1 Thats the point!
Jichao
@jcyang an array is not a kind of list; array items are sequential in the computer's memory: it means that after array[0] there is always array[1] in the computer's memory. A list does not have this constraint - between the first and second elements in a list there could be other data completely unconnected to it. The worst-time complexity of the algorithm for insertion is O(n) in both cases but for completely different reasons (and on paper - in completely different cases - consider inserting `1` in list `0->2->3->4` versus in array `0 2 3 4`).
laura
its somewhat ambiguous when some languages call arrays lists, it would be better to explictly say linked list if that is what is meant - expanded my comment to an answer as it seems to be helpful
jk
A: 

The worst case insertion time in a sorted list is O(n). The worst case is inserting the highest item into the list. To do that you have to step through all the elements, then insert at the end. The reason you don't get to do a binary search is that only element that you can access in the list is the successor of your current element, i.e. no random access.

Joel
A: 

Wikipedia is correct. As others here have already stated, lists are not random-access, hence you need to visit each node between A and B before you get to B. That makes binary search useless as traversing the list is O(n), so you end up doing more work than if you just iterated over the list once. You could cache the start, middle and end nodes in a separate buffer and check those first. However, that would just have the same effect as using multiple lists. The skip-list data structure takes that idea one step further.

So use a random-access heap instead, or perhaps a skip-list: http://en.wikipedia.org/wiki/Skip_list depending on your needs.

Mads Elvheim
A: 

if the it's linked list backed you can't do binary search so; finding the insertion point is O(n), actually inserting is O(1) as you just change the adjacent nodes, overall O(n).

if its array backed you can do a binary search so; finding the insertion point is O(log(n)), but inserting in an array is O(n) as you may have to shift all the elements of the array, overall O(n)

this is why you actually have tree/heap backed so all operations can be O(log(n))

jk
thanks.i didn't consider the movement of nodes before :)
Jichao