Timsort

https://archive.codeplex.com/?p=timsort4net#117964

download archive https://codeplexarchive.blob.core.windows.net/archive/projects/timsort4net/timsort4net.zip

Project Description
TimSort is relatively new sorting algorithm invented by Tim Peters in 2002, which is a hybrid of adaptive MergeSort and InsertionSort. It is not worse than QuickSort which modified version is used as default sorting algorithm in .NET. TimSort's average case performance is O(n log n) (same as QuickSort) but both best case and worst case performances are bettern then QuickSort: O(n) and O(n log n) respectively (QuickSort is O(n log n) and O(n^2)).
This implementation is a C# translation of Josh Bloch's Java implementation of TimSort.
Wikipedia: http://en.wikipedia.org/wiki/Timsort
Josh Bloch's Java implementation: http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/TimSort.java?view=co
TimSort takes advantage of two facts:
data we sort is often already partially sorted
comparison might be expensive (complex Compare method) while swapping elements is cheap (swapping pointers)
When comparison gets slower (for example, comparing complex objects) TimSort increases its advantage over QuickSort.
Array of 5368709 items, quick compare function: (actually: int.CompareTo(int))
Random data
Builtin: 1859.4927ms
TimSort: 1773.2582ms
Generally ascending data (80% chance that next item is greater than previous one)
Builtin: 1166.6400ms;
TimSort: 247.1780ms
Generally descending data (80% chance that next item is smaller than previous one)
Builtin: 1190.5521ms
TimSort: 571.7132ms
NOTE: TimSort's performance is the same for random data, but is significantly better for already ordered data.
Array of 536870 items, quite slow compare function (I actually added Thread.Sleep(0) to it):
Random data
Builtin: 6117.1032ms
TimSort: 4578.1129ms
Generally ascending data (80% chance that next item is greater than previous one)
Builtin: 5323.7977ms
TimSort: 841.0910ms
Generally descending data (80% chance that next item is smaller than previous one)
Builtin: 5321.9103ms
TimSort: 1094.5267ms
NOTE: TimSort's performance is better than QuickSort's when compare function is slow, even for random data.

Python's listobject.c – the C implementation of Timsort used in CPython
https://github.com/python/cpython/blob/master/Objects/listobject.c

https://sikasjc.github.io/2018/07/25/timsort/

http://hg.savannah.gnu.org/hgweb/octave/file/0486a29d780f/liboctave/util/oct-sort.cc

https://csharp.hotexamples.com/examples/-/List/TimSort/php-list-timsort-method-examples.html

posted @   scott_h  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示