向 TList 和 TStringList 添加稳定排序的简单方法
我将 TList/TObjectList 和 TStringList(带有关联对象)用于多种任务,或者按原样使用,或者作为更复杂结构的基础。虽然排序功能通常足够好,但有时我需要稳定 排序,两个列表都使用快速排序。
为 TList 和/或 TStringList 实现稳定排序的最简单方法是什么?我是否必须编写自己的排序例程,或者可以通过使用 TStringListSortCompare/TListSortCompare 的一些巧妙技巧来完成吗?
I use TList/TObjectList and TStringList (with associated objects) for a multitude of tasks, either as-is, or as basis for more complex structures. While the sort functionality is usually good enough, I sometimes need to do a stable sort, and both lists use quicksort.
What's the easiest way to implement stable sorting for TList and/or TStringList? Do I have to write my own sorting routine, or can it be done by using some clever trick with TStringListSortCompare/TListSortCompare?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须编写自己的排序例程。
您可以阅读当前的 QuickSort 实现,并编写自己的稳定版本(例如合并排序或任何其他稳定变体< /a>)。
一些技巧:
Count
,您可以使用快速指针数组 (TList.List[]
),而不是较慢的Items[]
或GetItem()
:子方法调用和范围检查会减慢执行速度;You'll have to write your own sorting routine.
You can read the current QuickSort implementation, and write your own stable version (e.g. a Merge sort or any other stable variant).
Some tricks:
Count
, you can use the fast pointer array (TList.List[]
) instead of slowerItems[]
orGetItem()
: sub-method calling and range checking slow down the execution;这个问题相当老了,但这是我对未来读者的回答:
我最近也需要这个,并改编了 Julian Bucknall 所著的《The Tomes of Delphi Algorithms and Data Structures》一书中的实现。 TList、TObjectList 及其后代类的实现。它适用于 Delphi 2009 到 XE7 以及可能其他版本:
http://alexandrecmachado.blogspot.com.br/2015 /02/merge-sort-for-delphi.html
This question is rather old, but here goes my answer for future readers:
I also needed this recently and adapted the implementation found in the book "The Tomes of Delphi Algorithms and Data Structures" by Julian Bucknall. Implementation for TList, TObjectList and descendant classes. It works with Delphi 2009 to XE7 and probably other versions as well:
http://alexandrecmachado.blogspot.com.br/2015/02/merge-sort-for-delphi.html
来自类似的问题 How Can I Replace StringList .在 Delphi 中使用稳定排序进行排序?,在 lkessler 的评论中链接到这里,我需要将问题中提到的非常简单的技巧复制到这里。
只需将初始订单号添加到要排序的数据中,并在 CustomSort 比较函数中添加最后一个比较条件来比较该初始订单号,即可轻松使快速排序表现稳定。
简单、快速、智能。每个可排序项仅花费一个额外的整数(或字节,或者如果对 TComponent 进行排序,则使用一些保留存储,如 TComponent.Tag),并对其进行一次初始化循环。
From similar question How Can I Replace StringList.Sort with a Stable Sort in Delphi?, linked here in comment by lkessler I need to copy to here really easy trick as mentioned in question.
You can easily make quick sort behave stable just by adding initial order numbers into data to sort and adding last comparation condition in CustomSort compare function to compare this initial order numbers.
Easy, fast and smart. Costs only one extra integer (or byte, or use some reserved storage like TComponent.Tag if You sort TComponents) on each sortable item and one initialization loop over them.
对于任何使用泛型的人来说,这里是插入和合并排序的现成实现,这两种算法都是稳定的排序算法。
该实现是针对数组进行的,也适用于 TList/TObjectList(因为它们的 Items 属性是一个数组)。
除了稳定之外,根据我的经验,这种合并排序实现确实比内置快速排序表现出更好的性能(尽管它使用更多内存)。
For anyone using generics here is a ready-to-use implementation of insertion and merge sort, both stable sorting algorithms.
The implementation is made for arrays and applicable for TList/TObjectList too (as their Items property is an array).
Besides being stable, in my experience, this merge sort implementation does show better performance than the build-in quick sort (though it uses more memory).