Linear sort of cardinal sort, bucket sort, count sort

Source: Internet
Author: User

      • Base sort
      • Count sort
      • Bucket sort

The Cardinal sort, the bucket sort, the count sort is three kinds of linear sorting method, broke through the comparison sort O (Nlogn) limit. But only for a specific situation.

Base Sort

The following is a description of Wikipedia:

Base sort:
Unify all the values to be compared (positive integers) to the same digit length, with a short number of digits preceded by 0. Then, start with the lowest bit and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.

The cardinality can be sorted by LSD (Least significant digital) or MSD (most significant
Digital), the LSD is sorted by the rightmost start of the key value, while the MSD is the opposite, starting with the leftmost side of the key value.

Efficiency of Cardinal Sort:
The time complexity of the radix sort is O (k n), where n is the number of ordered elements, and K is the number of digits. Note that this is not to say that this time complexity must be better than O (n log (n)), the size of K depends on the choice of digital bits (such as the number of bits), and the size of the complete set of data types to be sorted, K determines how many rounds are processed, and N is the number of operations per round.

To sort n different integers for example, assuming that the integers are based on B, so that each digit has a B different number, k =
LOGB (n), n is the potential of the complete collection of data types to be sorted. Although there are b different numbers that require B different buckets, in each round of processing, it is judged that each data item to be sorted only needs to be computed once to determine the value of the corresponding digit, so in each round of processing it takes an average of n operations to put the integers into the appropriate buckets, so there are:

K is approximately equal to LOGB (N) So, the average time t for the cardinality sort is:

t~= LOGB (N) • N one of the previous items is a constant independent of the input data, which is not necessarily less than the Logn

If you consider and compare the sort to compare, the form complexity of the cardinal sort is not necessarily smaller, but because it is not compared, so the cost of its basic operation is less, and under the appropriate choice of B, K is generally less than logn, so the base sort is generally faster than the comparison based on the sorting, such as fast sorting.

In simple terms, the cardinality sort is done from the lowest bit sort to the highest order. Time complexity O (kn) space complexity O (n)

#include <iostream>using namespace Std;intMaxbit (intData[],intN) {intD =1;intmax = data[0]; for(inti =1; I < n; i++) {//Find out the maximum value        if(Data[i] > max) max = data[i]; }intRadix =Ten; while(Max >= Radix)        {d++; Radix *=Ten; }returnD;}voidRadixsort (intData[],intN) {intD = maxbit (data, n);//Find the number of digits in the maximum number in the data, and decide to loop several times    int*Count=New int[Ten];//10 barrels    int*tmp =New int[n];intRadix =1; for(inti =0; I < D; i++) {//0 barrels per clear counter         for(intj =0; J <Ten; J + +)Count[j] =0;//Count the number of records in each bucket         for(intj =0; J < N; J + +) {intK = (Data[j]/radix)%Ten;Count[k]++; }//allocation of TMP locations         for(intj =1; J <Ten; J + +) {Count[j] =Count[J-1] +Count[j];//COUNT[J] is the number of records in a J-bucket and a smaller bucket than J}///backwards to collect all the records in the bucket in sequence to TMP         for(intj = N-1; J >=0; j--) {intK = (Data[j]/radix)%Ten; tmp[Count[K]-1] = Data[j];Count[k]--; }//Copy temporary array back to data         for(intj =0; J < N;        J + +) Data[j] = Tmp[j]; Radix *=Ten; } DeleteCount; Delete tmp;Count= NULL; TMP = NULL;}intMain () {intData[] = { the,353,458,279,129, the}; Radixsort (data,6); for(inti =0; I <6; i++) cout << Data[i] <<" ";}
Count Sort

Wikipedia-counting sort
Counting sorting is a large array of data, requiring a lot of time and memory. Suitable for sorting integers within a small range.
The steps of the algorithm are as follows:

    • Find the largest and smallest elements in the array to be sorted
    • Count the number of occurrences of each element in the array of I, stored in the item I of array C
    • Summation of all counts (starting with the first element in C, each item and the previous item are added)
    • Reverse-Populate the target array: Place each element I in the C (i) of the new array, subtract C (i) by 1 for each element placed
void Count_sort (intData[],intN) {int *tmp= newint[n];int Max= data[0];//MAX is the maximum number in data    int min= data[0]; for(inti =0; I < n; i++) {if(Data[i] >Max)Max= Data[i];if(Data[i] <min)min= Data[i]; }intLen =Max-min+1;int *count= newint[Len]; for(inti =0; i < Len; i++) Count[i] =0; for(inti =0; I < n; i++) count[data[i]-min]++; for(intj =1; J < Len; J + +) Count[j] + = count[j-1]; for(inti = n-1; I >=0; i--) {tmp[count[data[i]-min] -1] = Data[i]; count[data[i]-min]--; } for(inti =0; I < n; i++) Data[i] = Tmp[i];DeleteCountDeleteTMP;}
Bucket Sort

Suppose that the range of the sort number is [0,1], which is divided in [0,1] by an average of k buckets, and the number to be sorted is placed in those buckets. In-bucket ordering (common insert sort) for the number of elements in the bucket greater than 1. Because the average partition, so the elements within a bucket will not be too much, time complexity O (n), click to open Wikipedia-bucket sort

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linear sort of cardinal sort, bucket sort, count sort

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.