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 @ 2019-10-10 10:24  scott_h  阅读(220)  评论(0编辑  收藏  举报