I am working on a phonebook application where I want to sort the nodes that represent each entry according to the name of the client. I want to use bucket sort algorithm where I have two arrays of the linked list’s structures.
The catch is that I do not want to use deletion and construction to move the nodes through the buckets.
Is there an easier way, using pointers? Moving the pointers through the buckets might be much easier, but I don’t have any idea about how to implement it. I’m doing this in C, but help in any other language is welcome.
I appreciate your help.
This really depends on how the entries are stored. If you’re storing each entry in a linked list cell, then you can do bucket sort with only a constant memory overhead by just moving the elements out of the original list and into the buckets. This will require you to use pointers and pointer rewiring, but it’s not nearly as difficult as it sounds. You’ll just be splicing the cells out of the main list and into the buckets for the sort.
One question – is there a reason that you want to use bucket sort to sort names? You can sort strings using bucket sort, but to do so you’ll almost certainly need more than two buckets; probably you’d have one for each letter of the alphabet and one for “there are no letters here.” If you have a linked list, you may want to consider investigating merge sort as a possibility, since it’s not too difficult to implement and has excellent runtime guarantees.