On 15-03-08, nha pham wrote: > > 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 don't understand how this is an improvement, since with binary search the idea is that each comparison cuts the remaining list to search in half; i.e., each comparison yields one bit of information. Here, you're spending a comparison to cut the list to search at the element you just inserted, which is probably not right in the middle. If you miss the middle, you're getting on average less than a full bit of information from your comparison, so you're not reducing the remaining search space by as much as you would be if you just compared to the element in the middle of the 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. For all that, though, experiment trumps theory... > Here is detail about algorithm and testing: > https://github.com/nhapq/Optimize_binary_insertion_sort > > Sincerely. > > phqnha _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com