"stathis gotsis" <stathisgotsis@hotmail.com> writes:[color=blue]
> "Carl R. Davies" <nwsgrp1@googlemail.com> wrote in message
> news:1141315734.006824.84520@z34g2000cwc.googlegro ups.com...[/color]
[...][color=blue][color=green]
>> If yes, then I believe this is O(n). i.e. you're potentially looking at
>> every element in the list if your insert value is greater than any
>> value in the list. This is slow.[/color]
>
> This is insertion sort, and it is O(n^2) when sorting n elements and yes it
> is the worst you can get.[/color]
There are sorting algorithms *much* worse than O(n^2), but as far as I
know only deliberately perverse ones.
[color=blue][color=green]
>> Searching will also be O(n), you have to look at potentially every
>> element in the list. I guess you can exit when you know you're past
>> your value.[/color]
>
> O(logn) and no you do not have to look at every element in the list even in
> he worst case.[/color]
Searching a linked list is O(n); you can't get to any node without
first traversing all the previous nodes. (The number of nodes you
visit will be n/2 on average; that's still O(n), of course.) A binary
search on a sorted array is O(log n).
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.