简体   繁体   English

使用指针和两个结构数组进行存储桶排序

[英]Bucket Sort using pointers & Two Arrays of structures

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. 我正在用C语言执行此操作,但是欢迎使用其他任何语言的帮助。

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. 如果您有一个链表,您可能希望考虑研究合并排序,因为它实施起来不太困难并且具有出色的运行时保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM