Reading diary-Linear time Sorting algorithm

Source: Internet
Author: User

In the worst case, any comparison sorting algorithm requires an O (NLGN) comparison.

However, under the specified conditions, the sequencing algorithm of linear time can make the sorting complete in O (n) time.

Count sort

  Suppose that each of the N input elements is an integer within the range of 0 to K, where K is an integer. K=o (n), the sort run time is O (n).

Main ideas:

Create an array of length k, and use the value of the corresponding input array A as an index to count the occurrences of each subscript in the K array.

The code is as follows:

 

voidCounting_sort (int* Intarr,int* Outarr,intKintLen) {    int* CARR =New int[K]; memset (CARR,0, k*sizeof(int)); //Store the number of occurrences of each value with Carr     for(intI=0; i<len; ++i) {Carr[intarr[i]]+=1; }        //stores the current value exists, the size of the number of bits     for(intI=1; i<k; ++i) {Carr[i]=carr[i]+carr[i-1]; }         for(inti=len-1; i>=0; --i) {outarr[carr[intarr[i] ]=Intarr[i]; Carr[intarr[i]]-=1; }        Delete[] cArr;}

Base sort

Suppose you sort n d-bit integers, from low to high, D-rounds. Because each bit value is 0~9, it is necessary to sort the ordering method of the D-Times K to 9.

Thus the total time of the Cardinal sort is O (d (n+k)).

The code is as follows:

   

typedefstruct_basevar{intidx; int var;} Basevar;//Adjusted count sortvoidCOUNTING_SORT_EX (basevar* intarr,basevar* Outarr,intKintLen) {    int* CARR =New int[K]; memset (CARR,0, k*sizeof(int)); //Store the number of occurrences of each value with Carr     for(intI=0; i<len; ++i) {carr[intarr[i].var] +=1; }        //stores the current value exists, the size of the number of bits     for(intI=1; i<k; ++i) {Carr[i]=carr[i]+carr[i-1]; }         for(inti=len-1; i>=0; --i) {outarr[carr[intarr[i].var]].var=intarr[i].var; Carr[intarr[i]]-=1; }        Delete[] cArr;}//voidRadix_sort (int* Intarr,intDintLen) {Basevar* Vararr =NewBasevar[len]; Basevar* Outarr =NewBasevar[len]; //D-Bit loop     for(intj=0; j<d; ++j) { for(inti =0; i<len; ++i) {vararr[i].var= (intarr[i]>>d) &1;//take the value of each bitVararr[i].idx = i;//index before the record is sorted        }        //Count SortCOUNTING_SORT_EX (Vararr,outarr,Ten, Len);  for(intI=0; i<len; ++i) {Intarr[outarr[i].idx]= Intarr[vararr[i].idx];//assigns values on the original index based on the new index        }    }        Delete[] Vararr; Delete[] outarr;}

Bucket sort

Assuming that the input data is uniformly distributed, the time cost for the average is O (n).

Main ideas:

The data is divided into n equal-sized intervals, called buckets. The input is then distributed as evenly as possible to each bucket by mapping function f (k). Finally, each bucket is sorted separately,

Then walk through each bucket and list the elements in each bucket in order.

Code slightly.

Reading diary-Linear time Sorting algorithm

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.