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.
我正在编写一个电话簿应用程序,我希望根据客户机的名称对表示每个条目的节点进行排序。我想使用bucket排序算法,我有两个链表结构的数组。
The catch is that I do not want to use deletion and construction to move the nodes through the buckets.
问题是,我不希望使用删除和构造来在bucket中移动节点。
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.
有没有更简单的方法,使用指针?将指针移动到桶中可能要容易得多,但是我不知道如何实现它。我是用C语言写的,但是用其他语言来帮助是很受欢迎的。
I appreciate your help.
我很感激你的帮助。
1 个解决方案
#1
2
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.
这实际上取决于条目是如何存储的。如果将每个条目存储在一个链表单元格中,那么只需将元素从原始列表中移到bucket中,就可以使用一个常量内存开销来进行bucket排序。这将需要您使用指针和指针重新布线,但这并不像听起来那么困难。你只需要将这些单元格从主列表中分离出来,并将它们放入桶中进行排序。
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.
有一个问题——您想要使用bucket排序来排序名称,这有什么原因吗?您可以使用bucket排序对字符串进行排序,但是要做到这一点,您几乎肯定需要两个以上的bucket;可能每个字母都有一个字母,还有一个字母代表“这里没有字母”。如果您有一个链表,那么您可能需要考虑调查merge sort,因为它实现起来并不困难,并且具有良好的运行时保证。
#1
2
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.
这实际上取决于条目是如何存储的。如果将每个条目存储在一个链表单元格中,那么只需将元素从原始列表中移到bucket中,就可以使用一个常量内存开销来进行bucket排序。这将需要您使用指针和指针重新布线,但这并不像听起来那么困难。你只需要将这些单元格从主列表中分离出来,并将它们放入桶中进行排序。
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.
有一个问题——您想要使用bucket排序来排序名称,这有什么原因吗?您可以使用bucket排序对字符串进行排序,但是要做到这一点,您几乎肯定需要两个以上的bucket;可能每个字母都有一个字母,还有一个字母代表“这里没有字母”。如果您有一个链表,那么您可能需要考虑调查merge sort,因为它实现起来并不困难,并且具有良好的运行时保证。