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
Timsort的更多相关文章
- 简易版的TimSort排序算法
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. 简易版本TimSort排序算法原理与实现 TimSort排序算法是Python和Ja ...
- TimSort in Java 8
在项目中使用了Collections.sort(list, comparator)对集合进行了排序,偶然间遇到异常IllegalArgumentException: "Comparison ...
- TimSort算法分析
Timsort是一种混合稳定的排序算法,采用归并排序混合插入排序的设计,在多种真实数据上表现良好. 它基于一个简单的事实,实际中大部分数据都是部分有序(升序或降序)的. 它于2002年由Tim Pet ...
- Java TimSort算法 源码 笔记
本来准备看Java容器源码的.但是看到一开始发现Arrays这个类我不是很熟,就顺便把Arrays这个类给看了.Arrays类没有什么架构与难点,但Arrays涉及到的两个排序算法似乎很有意思.那顺便 ...
- Timsort 算法
转载自:http://blog.csdn.net/yangzhongblog/article/details/8184707 Timsort是结合了合并排序(merge sort)和插入排序(inse ...
- JDK(二)JDK1.8源码分析【排序】timsort
如无特殊说明,文中的代码均是JDK 1.8版本. 在JDK集合框架中描述过,JDK存储一组Object的集合框架是Collection.而针对Collection框架的一组操作集合体是Collecti ...
- TimSort学习资料
深入理解 timsort 算法(1):自适应归并排序 如何找出Timsort算法和玉兔月球车中的Bug? Java TimSort算法 源码 笔记 Timsort https://en.wikiped ...
- TimSort Java源码个人解读
/*JDK 1.8 */ package java.util; /** * A stable, adaptive, iterative mergesort that requires far fewe ...
- Arrays.sort() ----- TimSort
Arrays.sort() Arrays.sort()对于基本类型使用的是DualPivotQuicksort双轴快速排序,而对于非基本类型使用的是TimSort,一种源自合并排序和插入排序的混合稳定 ...
随机推荐
- Python的is和==
is是对比地址:==是对比值
- nodejs爬虫案例笔记
用nodeJs制作一个简单的网页爬虫 主要分为三个步骤,向目标请求数据,处理数据,打印数据.需要用到的模块有http,cheerio. 1.准备步骤,引入要使用的模块 2.向目标请求数据 http.g ...
- 如何利用awk计算文件某一列的平均值?
[root@master yjt]# cat yjt.sh #!/bin/bash awk -v field="$1" '{sum+=$field; n++;}END {if (n ...
- 个人学习分布式专题(二)分布式服务治理之Dubbo框架
目录 Dubbo框架 1.1 Dubbo是什么 1.2 Dubbo企业级应用示例(略) 1.3 Dubbo实现原理及架构剖析 1.4 Dubbo+Spring集成 Dubbo框架 1.1 Dubbo是 ...
- git补充(命令)转自https://github.com/Wasdns/github-example-repo
在使用命令行进行提交时,通常使用git commit -m '注释信息'来填写commit注释信息,但是-m参数适合单行注释,对于多行的commit注释来说是不合适的.这里推荐使用git commit ...
- STM32F4 LTDC
首先配置同步时序先看参考手册 下面看一个实际例子,一块439的开发板 设置: 配置时序 LTDC_InitStruct.LTDC_HorizontalSync = ; /* */ LTDC_InitS ...
- 微信小程序之分享功能
说到分享 大家都会想到手机右上角点击不就分享了么?对的没错,那样是分享转发的是小程序 而不是指定的某个页面,所以自己动手丰衣足食,自己写一个转发功能被, 其实也没那么可怕,主要参考的是微信小程序AP ...
- 相关 Excel 开源类库性能对比
性能数据 · Excelize 中文文档https://xuri.me/excelize/zh-hans/performance.html Golang library for reading and ...
- 安卓P2P开源项目
https://github.com/LinYaoTian/P2PChat 一个基于局域网的 Android P2P 聊天系统 https://github.com/ddssingsong/webrt ...
- C# [ThreadStatic] 标记静态字段对多线程执行的影响
类的静态字段在类的实例中是共享的.多个线程修改实例字段的值在对其它线程来说是可见的,这也是clr默认的行为.对静态字段添加ThreadStaticAttribute标记可以改变这种默认的行为. Thr ...