Old text-bitsort sorting algorithm-2007-10-10 16:08

Source: Internet
Author: User

"Programming Pearls" in the introduction of the algorithm. the complexity of the algorithm is O (x), and X is the maximum number to sort. Not suitable for sorting with duplicate data, the duplicated data is treated as a data output.

Or look at the code!

C Program implementation:
Selected from "Programming Pearls"

/*bitsort.c--bitmap sort from Column 1 * sort distinct integers in the range [0. . N-1]*/#include<stdio.h>#defineBitsperword 32//define each int type to hold 32 bits  #defineSHIFT 5//32=2^5  #defineMASK 0x1F//5-bit masking  #defineN 10000000//bit arrays total number of bits    inta[1+ N/bitsperword];//bit arrays are implemented using an int array.     void   Set(inti) {A[i>>shift] |= (1<< (I & MASK)); }//Place 1 position i           /*extract the first bit of the array because each int holds 32 bits, the integer part of I/32 is the element in the int array, and the remainder part of the displacement Substitution Division I/32 is the corresponding number of bits in the int array.               For example i=61, then I/32 = 1 means i=61 in the a[1] element, i/32 remainder is 29=i&mask, which means that the 29th bit in the a[1] element is saved */  voidclrinti) {A[i>>shift] &= ~ (1<< (I & MASK)); }//Clear 0 The first I bit  intTestinti) {returnA[i>>shift] & (1<< (I & MASK)); }//extract the value of the I-bit     intMain () {inti;  for(i =0;   i < N; i++) CLR (i); //set each bit of the array to 0//because bit-wise operation is not efficient, you can use the following three lines to assign values to an array of integers instead                /*Replace above 2 lines with below 3 for word-parallel init int top = 1 +                  N/bitsperword;   for (i = 0;   i < top;                  i++) A[i] = 0; */                   while(SCANF ("%d", &i)! = EOF)//set the corresponding bit according to the value entered                   Set(i);  for(i =0;   i < N; i++)//detect which one is 1                   if(Test (i)) printf ("%d", i); return   0; }   


Python language implementations:
Excerpt from: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/528942

1 #Python implementation of Bitsort algorithm from "Programming Pearls"2 #http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/5289423 4 defbitsort (filename, MAXN):5     """Sort a file named ' filename ' which6 consists of maxn integers where each7 integer is less than MAXN"""8 9     #Initialize BitmapTenA = [0]*MAXN One  A     #Read from file and fill bitmap -      forLineinchfile (filename): -n =Int (Line.strip ()) the         #Turn bits on for numbers -         ifN<maxn:a[n] = 1 -  -     #Return A generator that iterates over the list +      forNinchRange (len (a)): -         ifA[n]==1:yieldN +  A      at if __name__=="__main__": -     #numbers.txt should contain a list of numbers -     #Each of the than 1000000, one per line. -      forNuminchBitsort ('Numbers.txt', 1000000): -         PrintNum


Old text-bitsort sorting algorithm-2007-10-10 16:08

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.