Link to home
Start Free TrialLog in
Avatar of 4eyesgirl
4eyesgirl

asked on

Fastest search/sort algorithm for a link list data structure

Hi expert,

If I have a link list data structure - the fastest search algorithm will be linear search still?  I thought about binary search, but can I use it with the link list data structure?  
Usually binary search will jump index and I don't think it would works for link list

Also, what is the appropriate sort method I should use?  Can I use quicksort?

Thanks
Avatar of jkr
jkr
Flag of Germany image

How "dynamic" is that list structure? You might be fine by "indexing" the whole list if it is quite static, on th eother hand, this might get inconvenient if that list constantly changes. A good tradeoff might be to track pointers the elements in a  std::map, so you can look them up easily as well as taking care of changes in that list.
Even a std::vector combined with the STL sort algorithms (or your own if you prefer) might be sufficient.

About binary search : as you know : that works pretty well on sorted data (O(log(N)) as opposed to O(N) for linear search).

Then there's interpolation search, which works even better for large (sorted) lists with even distribution (between O(log(log(N))) and O(N)).

If you don't want to sort the list, you can do better than linear search by using Grover's algorithm for example (O(N^(1/2))).

As jkr suggested, you could also use specialized data structures, like hash tables, associative maps, (self-balancing) binary search trees, etc.

Some links on the mentioned algorithms/data structures :

http://en.wikipedia.org/wiki/Linear_search
http://en.wikipedia.org/wiki/Binary_search
http://en.wikipedia.org/wiki/Interpolation_search
http://en.wikipedia.org/wiki/Grover%27s_algorithm

http://en.wikipedia.org/wiki/Associative_array
http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree
The best choice for you depends largely on the type of data, frequency of data modification (addition, deletion, etc.), frequency of access, etc.
The short answer is, if you have a singly-linked (or even double-linked) list, with no extra data structures added for indexing purposes, the best you can do is to search it item-by-item in one direction or another, which requires linear time, as you observed.

The longer answer is that you can use STL algorithms and classes to speed things up if you just want it to work in faster than linear time.

The yet longer, but more complete answer is, if you're dealing with a process wherein items can be encountered and inserted into (and possibly deleted from) the linked list in a random order, then you have to deal with some overhead at insertion (and deletion) time in order to ensure your data is indexed (or linked) in a way that is conducive to efficient searching.  The answer to "what's the fastest search algorithm" is a misleading one.  What you really need is to profile the sort of data your program will be encountering, and then analyze the performance of your process to make the _process_ as fast as possible.  An example: if your program typically performs a million searches between each data insertion, then it's most effective for you to spend lots of time setting up and maintaining efficient indexing structures to make the searches as fast as possible.  On the other hand, if your program does a gazillion insertions, and only occasionally does a search, spend less time on maintaining indexing structures, knowing that while it'll make the searches slower, you'll win in the long run because your insertions will take less time.  The tricky part is that insertions (and deletions) typically also require searching the structure, so you have to balance the search time with insertion/deletion overhead.

IMO, chances are good that if you just go with the STL you'll be happy.  If not, and you want to use a custom data structure rather than going with the STL, implement a self-balancing binary tree (red/black trees are good) instead of a linked list.  Insertion, deletion, and searching are all O(log n) operations.  It's about the most efficient, easy-top-implement, general-purpose ADT you can get.  If that's still not good enough, you can try a B-tree or R-tree, which sacrifice some search time (depending on the circumstances) in favor of less maintenance overhead.
>>>> what is the appropriate sort method I should use?

The best for linked lists is if they are already sorted. You should insert new items so that the list is always sorted. As a list is very quick for inserting between, that gives the best overall performance.

Note, making a sorted insert into a singly linked list gives very bad results if the values are already sorted. Then, you need to compare each new item with each existing item, hence it is similar (bad) as a bubble sort. In case of a doubly linked list you could check the first and last list entry to decide whether you run from front to back or back to front  to find the insertion point.

Often you may get the values from an other container or a database. If so, you might check whether that other container can provide a sorted list. If so you simply could build the sorted list by adding nodes at the back of the list and only for additional entries you have to lookup for  the insert position.

Regards, Alex
Avatar of 4eyesgirl
4eyesgirl

ASKER

I guess I wasn't clear about my question -

I am just building a simple double linked-list and I cannot use the stl.  

To make it simple, let's assume I have a data structure that has 4 items (firstname, lastname, email and DOB).

I was thinking to do linear search since the data structure is a link-list, I don't think I will go for the indexing approach as this is a fairly small program.

Then my next concern is sorting - I do plan to keep the record in a sorting order (by firstname and last name).  but I also want to provide a sorting feature if user decide they want to print out the link list contents sorted by email address or DOB.  In this case, even if I have a sorted- link -list, I will have to go through the entire list and create a new link list and sorted by DOB or email.  Does it make sense to you?  I have a pretty good idea how to do it, but I would like your expert opinion.

Thanks.
Unfortunately, if you're required to use your double-linked list, and don't want to add indexing overhead, then inserting elements in sorted order is an O(N) process, with average time taking N/2.  Searching for an element is also an O(N) process, with the same average time.  Printing in that sorted order is an O(N) process as well, and printing sorted by any other criteria is at best an O(N*log(N)) process.

So, building the list is O(N^2) process (because each insertion is O(Ni), where Ni is the number of elements in the list at that time, which means the process is at worst O(sum(Ni)), which would be O((N*(N+1))/2), or O(N^2)) if you build it in sorted order.  Average time for an insertion at each list size would be Ni/2, so the average time to build the list is N(N+1)/4.  Since printing in sorted order would be O(N*log(N)), the upshot is that unless you're planning on printing the list (N+1)/4 times or more, it's faster to keep the list in unsorted order (assuming inserting at the beginning or end is an O(1) operation), and copy the contents of the list to a dynamically-created array to quicksort it every time you need to print a sorted list.

My numbers may not be accurate, but the bottom line is that if you choose to keep it in some sort of sorted order, linear time is the best you can do for insertions.  And, I think the fastest way to sort it by any other criteria will be to build an array, copy over the contents of the linked list, and quicksort it.  You can then recreate the linked list from that array in O(N) time, if you want.
If you need to sort the same list on different keys, you can always add more than 1 pointer to the nodes.

For example :

typedef struct node {
  string firstname;
  string lastname;
  string email;
  string DOB;
  struct node *next_firstname; // ordering by firstname
  struct node *next_lastname; // ordering by lastname
}
Make that of course :

typedef struct node {
  string firstname;
  string lastname;
  string email;
  string DOB;
  struct node *next_firstname; // ordering by firstname
  struct node *next_lastname; // ordering by lastname
} node;
smidgie82:-

Do you think there is any benefit if I keep the link-listed sorted anyway?  For example, if I want to check (perform a search) for insertion (make sure there is no duplicate?
Or as you would suggested, there will not be much benefit to keep the link list sorted?  Since I will also need to create or copy to another sorted link-list or data structure(array) and sorted by any field of the structure.

inifinity08 - I am not really understanding what you are saying.  Are you suggested me to have multiple node* for different order.  In this simple strcture, I would have 4 pointer to organize the link-list, and I think it is kind of messy.  Maybe I am not getting what you are suggesting?
SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help!  It was all very helpful suggestion.

I slit the points to the answer that I understand the most!

Thanks so much!!!