Counting sort and bitmap sorting

Source: Internet
Author: User

The Count sort (counting sort) is a stable linear time sorting algorithm . The count sort uses an extra array of C, where the I element is the number of elements in the array A to be sorted with the value equal to I. The elements in a are then ranked in the correct position according to the array C. The count sort is not a comparison sort, and the sort is faster than any comparison sort algorithm. Because the length of the array C used to count depends on the range of data in the array to be sorted (equal to the difference between the maximum and minimum values of the array to be sorted plus 1), this makes the count sort for arrays with a large data range, which requires a lot of time and memory. The count sort is more appropriate for sorting small-range collections. For example, 1 million students take part in the college entrance examination, and we want to sort the math scores of the 1 million students (assuming a score of 0 to 100). The steps for counting sorting are as follows:

    1. Determines the size of the C array by determining the range of values in the array to be sorted;
    2. Computes the number of occurrences of each element in the array with a value of a[i], in the array C[a[i]];
    3. Re-populates the original array with the C array.

The implementation code for the Count sort is as follows, and it is important to note that the code is not compared to the elements within the array, but instead:

1 voidCountingsort (int*narray,intnSize)2 {  3     int*ncounting =New int[NSize];//ncounting is an accompanying array that records the number of occurrences of each number in the original array4memset (Ncounting,0x0,sizeof(int)*nSize); 5  6     inti; 7      for(i=0; I!=nsize; i++)  8ncounting[narray[i]]++;//calculates the number of occurrences of each number, for example, ncounting[5] = 3; indicates that the original array exists 3 x 59  Ten     intz=0;  One      for(i=0; i<=nsize; i++)   A     {   -          while(ncounting[i]-->0)//ncounting[i]>0 indicates that there is a number I in the original array -narray[z++] =i;  the     }   -   - delete[] ncounting; -  +     return;  -}

The code is well understood, but there are still a few things to note:

    1. The count sort is typical for space-time, and the range of elements within the array determines the size of the accompanying array C, which determines the time and memory size of the program's run.
    2. When calculating c[i], using a[i] as subscript, you must ensure that all elements in a array are nonnegative, and if there is a negative value, you can add the absolute value of the smallest negative value to the array element, making the element all positive.

When the value range in the array A is large, the larger the accompanying array C, the greater the memory consumption, and the more restrictive the memory size, which can be sorted using the bitmap method (Bit-map). Suppose you want to sort the number of 5 numbers (4,7,2,5,3) within a range of 0-7, then we can use the Bit-map method to achieve the order. To represent 8 numbers, we only need 8 bit (1Bytes), first we open 1Byte space, all the bit bits of these spaces are set to 0, such as

And then traversing the 5 elements, first element is 4, then 4 corresponds to the position of 1, of course, the operation here involves Big-ending and little-ending case, where the default is big-ending), because it is zero-based, So to place the fifth position as one (e.g.):

Then the second element 7 is processed, the eighth position is set to 1, and then the third element is processed, until the final processing of all the elements, the corresponding position is 1, the state of the memory bit is as follows:

Finally we now traverse through the bit area, and the bit is the number output (2,3,4,5,7) of a bit, so that the order is reached. The above text is excerpted from http://www.cnblogs.com/Trony/archive/2012/09/01/2667064.html

The implementation is as follows:

1 voidSetbit (Char*pdata,intnnum)2 {3     //find the byte position you want to set4      for(intI=0; i<nnum/bytesize; i++)5pdata++;6     //set the bit bit corresponding to Nnum in bytes7* (PData) |=0x1<< (nnum%bytesize);8 }9 Ten voidBitmapsort (intArr[],intnSize) One { A     //a byte can represent 8 bits, and the size of the accompanying array is (nsize/bytesize) +1 -     Char*pdata =New Char[nsize/bytesize+1]; -memset (PData,0x0,sizeof(Char) * (nsize/bytesize+1)); the      -     //sets the corresponding bit bit in the accompanying array based on the value of each element in the original array -      for(intI=0; I!=nsize; i++) - setbit (PData, arr[i]); +  -     intz=0; +      for(intI=0; I!=nsize/bytesize+1; i++) A     { at         //Check for 8 bits in each byte -          for(intj=0; J!=bytesize; J + +) -         { -             if(*pdata & (0x1<<j)) -arr[z++] = i*bytesize+J; -         } inpdata++; -     } to  + delete[] PData; - } the  * int_tmain (intARGC, _tchar*argv[]) $ {Panax Notoginseng     intA[] = {3,1,2,5,9}; -  theBitmapsort (A,sizeof(a)/sizeof(*a)); +      for(intI=0; i!=sizeof(a)/sizeof(*a); i++) Acout<<a[i]<<"    "; thecout<<Endl; +     return 0; -}

Bitmap sorting is also a linear sort, and the dynamically allocated adjoint array takes up a lot less space than a count sort, but there is a drawback that bitmap ordering requires that there are no duplicate elements in the sorted array, because a bit bit can only be used with 0, which indicates whether an integer exists or not, and cannot represent the number of occurrences of this integer. , bitmap sorting uses a lot of bit operations, and code implementations are more complex than counting sorting.

The bitmap method can be used as a sort, as well as for matching and finding. Here is an example of a string containing (matching):

1 BOOLBmatchstring (Const Char*a,Const Char*b)2 {3     intNdictionary =0;4     intNlena = strlen (a), Nlenb =strlen (b);5 6     inti;7      for(i=0; I!=nlena; i++)8Ndictionary |= 0x1<< (a[i]-'A');//the corresponding bit bit is set9 Ten      for(i=0; I!=nlenb; i++) One         if(! (Ndictionary &1<< (b[i]-'A'))) A              Break; -  -     if(I! =Nlenb) the         return false; -     Else -         return true; - } +  - int_tmain (intARGC, _tchar*argv[]) + { A     CharA[] ="Youknowilveyouso"; at     CharB[] =" Love"; -  -     if(Bmatchstring (A, B)) -cout<<"A has B"<<Endl; -  -     return 0; in}

In fact, for a string match (including) program, define the adjoint array of int hash[26] (the element range as 26 letters), there is not too much memory consumption, this is just to illustrate the same bitmap method applies.

Counting sort and bitmap sorting

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.