[Algorithm learning] linear time sorting-counting sorting, base sorting, and bucket sorting

Source: Internet
Author: User
Tags sorts
Count sorting


Count sorting assumes that each of the N input elements is an integer between 0 and K. Here K is an integer (the input data is in a small range ).

AlgorithmThoughts

 

The basic idea of counting sorting is to determine the number of elements smaller than X for each input element x. Then place X in the position of the final output array.


Because the number of arrays may be equal, you need to pay attention to it during processing.


Time Complexity and space complexity analysis

 


Total algorithm timeBytes(K + n ). When K = O (n), the running time of counting sorting isBytes(N ).

The space complexity is O (n + k ). Two secondary arrays are required: the array B [N] for storing the sorting result and the C [k] for storing the temporary result.


Count sorting is a stable sorting algorithm.


Programming implementation (CPP)

// Counting sorting-Introduction to algorithms (version 2) p98 8.2 counting sorting // Author: Jiangnan Yanyu // E-mail: xiajunhust@gmail.com # include <iostream> # include <cstdlib> using namespace STD; void countsort (int * a, const int num, int * result) {int maxval =-99999; for (INT I = 0; I <num; ++ I) {If (maxval <* (a + I) maxval = * (a + I );} int * tempresult = new int [maxval + 5]; // records the intermediate result for (INT I = 0; I <maxval + 5; ++ I) * (tempresult + I) = 0; // result [I] records the number of elements whose values are equal to I in the array for (INT I = 0; I <num; ++ I) * (tempresult + * (a + I) = * (tempresult + * (a + I) + 1; // result [I] records the number of elements whose values are less than or equal to I in the array for (INT I = 1; I <maxval + 5; ++ I) * (tempresult + I) = * (tempresult + I) + * (tempresult + I-1); // note, the array may contain equal elements. // put the elements in the array directly into the correct position for (INT I = num-1; I> = 0; -- I) {* (result + * (tempresult + * (a + I) = * (a + I); * (tempresult + * (a + I )) = * (tempresult + * (a + I)-1;} Delete [] tempresult;} int main () {int num = 7; int * A = new int [num]; for (INT I = 0; I <num; ++ I) * (a + I) = rand (); cout <"before sort:" <Endl; For (INT I = 0; I <num; ++ I) cout <* (a + I) <"; cout <Endl; int * result = new int [num + 5]; countsort (A, num, result); cout <" after sort: "<Endl; For (INT I = 1; I <= num; ++ I) cout <* (result + I) <"; cout <Endl; delete [] A; Delete [] result ;}

 


Base sort


Algorithm IDEA

Base sorting sorts all numbers from low to high. If the maximum number of all digits is D, sort the values by the lowest valid digits to obtain a result. Repeat this process to a high level.

Note that the sorting by bit must be a stable sorting algorithm. Count sorting is often used.


Programming implementation (CPP)

 

// Base sorting // introduction to algorithms (version 2) P100 8.3 base sorting // Author: Jiangnan Yanyu // E-mail: xiajunhust@gmail.com # include <iostream> # include <cstdlib> # include <ctime> using namespace STD; // get the int getdigitnun (int A, int digit) of the I-th integer ); // sort by bit void digitsort (int * a, int N, int digit, int * result); // base Sorting Algorithm void radixsort (int * a, int N, int D); int main () {int n = 7, I; int * A = new int [N]; srand (Time (null); for (I = 0; I <n; ++ I) * (a + I) = rand (); // determines the maximum number. Int maxval =-1, D = 0; cout <"before sort:" <Endl; for (I = 0; I <n; ++ I) {cout <* (a + I) <""; maxval = maxval <* (a + I )? * (A + I): maxval;} cout <Endl; while (maxval> 0) {++ D; maxval/= 10;} radixsort (A, N, d); cout <"after sort:" <Endl; for (I = 0; I <n; ++ I) cout <* (a + I) <"; cout <Endl;} // base Sorting Algorithm void radixsort (int * a, int N, int D) {int * result = new int [n + 5]; // The bitwise sorting operation is executed cyclically for (INT I = 1; I <= D; ++ I) {digitsort (A, N, I, result); For (Int J = 0; j <n; ++ J) {* (a + J) = * (result + J + 1) ;}} Delete [] result ;}// obtain the I-digit integer int getdigitnun (int A, int digit) {While (-- digit) {A/= 10;} return a % 10;} // sort by bit // here void digitsort (int * a, int N, int digit, int * result) {// record intermediate result const int num = 15; int * tempresult = new int [num]; for (INT I = 0; I <num; + + I) * (tempresult + I) = 0; // initialization // tempresult [I] records the number of records in the array equal to I for (INT I = 0; I <n; ++ I) * (tempresult + getdigitnun (* (a + I), digit) = * (tempresult + getdigitnun (* (a + I ), DIGIT) + 1; // tempresult [I] records the number of smaller than the number equal to I in the array for (INT I = 1; I <num; ++ I) * (tempresult + I) = * (tempresult + I) + * (tempresult + I-1 ); // directly place the elements in the correct position for (INT I = n-1; I> = 0; -- I) {* (result + * (tempresult + getdigitnun (* (a + I), digit) = * (a + I ); * (tempresult + getdigitnun (* (a + I), digit) = * (tempresult + getdigitnun (* (a + I), digit)-1 ;} delete [] tempresult ;}

 

 

Time Complexity and space complexity analysis

Given n d-digits, the number of each digit may be K. If the stability of the bit-by-bit sorting time complexity isBytes(N + k), the complexity of the base sorting time isBytes(D (n + k )). Space complexity O (N + k ).

When D is a constant and K is O (n), the linear time complexity of the base sorting is involved.


There is another theorem about how to break each keyword into several digits:

Given n B dimensions and any positive integer r <= B, the base order can be inBytes(B/R) (N + 2 ^ r.

Here, for a value of r <= B, each keyword is regarded as a number with D = floor (B/R). Each number contains an R bit and then counts and sorts it.

The formula above can be deduced.Round (N)Complexity.

However, this does not mean that the base sorting is better than the comparison-based sorting algorithm, such as quick sorting! Because the constant factors hidden in the sign are different. Which sorting algorithm is better depends on the implementation features of the underlying machine. For example, fast sorting = usually makes more effective use of hardware cache. It also depends on the input data. In addition, the Count sorting is used as the intermediate stable sorting instead of in-situ sorting.


Sort buckets

When the input data is evenly distributed, it can run at a linear expected time. Even if the input does not meet the linear relationship, the bucket sorting can still run in linear time. As long as the input satisfies such a property, the sum of squares of each bucket size is linearly related to the total number of elements.

The thought of Bucket sorting:


The interval [0, 1) is divided into N subintervals of the same size, or buckets. Then, N input elements are distributed to each bucket. Each element in a bucket is stored in a linked list.

 


Programming implementation (CPP)

 

// Bucket sorting // introduction to algorithms (version 2) p102 8.4 bucket sorting // Author: Jiangnan Yanyu (2013-03027) // E-mail: xiajunhust@gmail.com # include <iostream> # include <cstdlib> # include <ctime> # include <cmath> using namespace STD; // type strudef CT structlinknode {double ELEM; struct structlinknode * Next;} linknode, * linknodeptr; // void bucketsort (double * a, int N); // delete a linked table void deletelinklist (linknodeptr head ); int main () {srand (Time (null); int n = 8; Double * A = new double [N]; for (INT I = 0; I <n; ++ I) * (a + I) = rand () * 1.0/rand_max; cout <"before sort:" <Endl; For (INT I = 0; I <n; ++ I) cout <* (a + I) <"; cout <Endl; bucketsort (A, n); cout <" after sort: "<Endl; For (INT I = 0; I <N; + + I) cout <* (a + I) <""; cout <Endl;} // void bucketsort (double * a, int N) {// linknodeptr * linklistarr = new linknodeptr [N]; // initialize for (INT I = 0; I <n; ++ I) {linklistarr [I] = new linknode; linklistarr [I]-> ELEM =-1; linklistarr [I]-> next = NULL;} // Add N input elements to N buckets in sequence for (INT I = 0; I <n; ++ I) {linknodeptr newnode = new linknode; newnode-> ELEM = * (a + I); newnode-> next = NULL; // Insert the new element into the correct position of the linked list of the corresponding bucket int Index = floor (N ** (a + I); linknodeptr loopptr = linklistarr [Index]-> next; linknodeptr prevptr = linklistarr [Index]; while (loopptr! = NULL & * (a + I)> loopptr-> ELEM) {prevptr = loopptr; loopptr = loopptr-> next;} newnode-> next = loopptr; prevptr-> next = newnode;} int COUNT = 0; For (INT I = 0; I <n; ++ I) {linknodeptr loopptr = linklistarr [I]-> next; while (loopptr! = NULL) {* (a + count) = loopptr-> ELEM; ++ count; loopptr = loopptr-> next ;}} for (INT I = 0; I <N; ++ I) deletelinklist (linklistarr [I]);} // delete a linked table void deletelinklist (linknodeptr head) {If (null = head) {return ;} deletelinklist (Head-> next); Delete head ;}

 

Time and space complexity analysis

 

The time complexity is O (n ).

The space complexity is O (n ). A secondary array is required to store buckets (linked lists ).


Even if the input does not meet the uniform distribution, the bucket sorting can still run in linear time, as long as the input meets the following condition: the sum of squares of each bucket size is linearly related to the total element.


Bucket sorting is a stable 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.