[Python-Dev] Tunning binary insertion sort algorithm in Timsort.

nha pham phqnha at gmail.com
Mon Mar 9 01:17:16 CET 2015


We can optimize the TimSort algorithm by optimizing its binary insertion
sort.



The current version of binary insertion sort use this idea:

Use binary search to find a final position in sorted list for a new element
X. Then insert X to that location.



I suggest another idea:

Use binary search to find a final postion in sorted list for a new element
X. Before insert X to that location, compare X with its next element.

For the next element, we already know if it is lower or bigger than X, so
we can reduce the search area to the left side or on the right side of X in
the sorted list.



I have applied my idea on java.util. ComparableTimSort.sort() and testing.
The execute time is reduced by 2%-6% with array of random integer.



Here is detail about algorithm and testing:
https://github.com/nhapq/Optimize_binary_insertion_sort



Sincerely.

phqnha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150308/9e3a5cd3/attachment.html>


More information about the Python-Dev mailing list