timsort--Merge sort of

Source: Internet
Author: User

Brief introduction

MergeSort the complexity of the input with the reverse sequence is O (n^2), and Timsort is for this situation, the mergesort is optimized for the average complexity of n*o (log n), the best case is O (n), the worst case n*o (log n). And Timsort is a sort of stability. The idea is to partition the sequence first and then merge the partitions to look like the mergesort step, but there are some optimizations for reverse and large-scale data.

Step Partitioning

The idea of partitioning is to scan an array once, put a continuous positive sequence (in ascending order, then the positive sequence is the ascending sequence), or "strict"(to ensure the stability of the sorting algorithm) as a partition (run), and if it is reversed, reverse the elements in the partition. For example
1,2,3,6,4,5,8,6,4 Partitioning results to
[1,2,3,6],[4,5,8],[6,4]
Then reverse the inverse sequence
[1,2,3,6],[4,5,8],[4,6]

Merge

Consider an extreme example, such as the length of the partition is 10000,10,1000,10,10, we would certainly like to have 10 10 merged into 20, 20 and 1000 merged into 1020 so, if from left to right in the order of merging, It is too expensive to use the 10000 array and the small array to merge each time. So we can use a strategy to optimize the order of merges.

Instance

As an example of Comparabletimsort.sort () in Java, a run stack is used to determine if a merge should be made.

        if (nRemaining < MIN_MERGE) {            int initRunLen = countRunAndMakeAscending(a, lo, hi);            binarySort(a, lo, hi, lo + initRunLen);            return;        }

Sort less than Min_merge (32), after partitioning directly with binary insertion sort

intMinrun = Minrunlength (nremaining); Do{//Find the starting position of the next partition, and also flip the reverse sequence            intRunlen = Countrunandmakeascending (A, lo, HI);//Ensure that run in the run stack is greater than minrun, and if the current partition is too small, extract the elements from the back to complement            if(Runlen < Minrun) {intforce = nremaining <= minrun?                Nremaining:minrun;                Binarysort (A, lo, lo + force, lo + runlen);            Runlen = Force; }//Put the run into the run stackTs.pushrun (lo, runlen);//To determine if it should be merged, I is starting at the top of the stack, knowing that it cannot be merged            //1 runlen[i-3] > Runlen[i-2] + runlen[i-1]            //2. runlen[i-2] > Runlen[i-1]Ts.mergecollapse ();            Lo + = Runlen;        Nremaining-= Runlen; } while(Nremaining! =0);//Merge All remaining runs to complete sort        assertLo = = Hi;//Merge the remaining runTs.mergeforcecollapse ();assertTs.stacksize = =1;

Look at one of the more important functions inside.

/*** If the length of the last 2 run is added longer than the previous one, then the run with the middle position and the shorter front and back length of the run merge * If the last 2 run has a shorter length than the previous one, merge the next 2 run * / Private void Mergecollapse() { while(StackSize >1) {intn = stackSize-2;if(N >0&& runlen[n-1] <= Runlen[n] + runlen[n+1]) {if(Runlen[n-1] < Runlen[n +1]) n--;            Mergeat (n); }Else if(Runlen[n] <= runlen[n +1]) {mergeat (n); }Else{ Break;//invariant is established}        }    }

timsort--Merge sort of

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.