Radix Sort Algorithm

蓝咒 提交于 2019-12-01 09:18:49

Algorithms are where you should start learning about programming!

To see what an undocumented piece of code does, you may need to adopt a pseudo-language to put the work into an english mathematics statement.

For example, you note that this snippet should only work for 8-bit numbers (the outer loop on bit). The rough description is that the array elements are "sorted" into two buckets depending on whether the bit in position "bit" is a zero or a one -- starting with the bit in the lease significant position. The original array is then reordered with "ones" coming before "zeros" .. which should order the array from largest to smallest.

You would be well served to look up radix sort algorithms, and start with that rather than starting with code.

Your algorithm works for 8 bit numbers, and you are looking at one bit at a time. An easier to understand example is the following, using decimal instead of binary.

Sort on 1s:    771 721 822 955 405   5 925 825 777  28 829
Sort on 10s:   405   5 721 822 925 825  28 829 955 771 777
Sort on 100s:    5  28 405 721 771 777 822 825 829 925 955

After we sort on the middle digit, observe that the numbers are sorted by their last two digits. After we sort on the most significant digit, the numbers are completely sorted.

The same goes for binary. The number of buckets we're using to sort is the SAME as the radix of the digit we use as a sort key during one pass of bucket or counting sort. "Radix" is a synonym for the base of a number, hence the name "radix sort." In your example, the number is 2.

A mask, or bitmask, is used to "turn off" every bit except those that are allowed to be "seen" through the mask. The 0s filter out the bit they are ANDed with, the 1s allow the bit through. A common usage is to isolate a byte-sized portion of an integer data type:

00000000 11111111 00000000 00000000
& // Boolean AND
10010101 10010101 10010101 10010101
yields
00000000 10010101 00000000 00000000
/*try this iterative method and 100% working*/
    #include<stdio.h>
    #include<math.h>
    int sort();
    int display();
    long int a[20],n;
    int main(){
    int i;
    printf("enter the size:");
    scanf("%d",&n);
    printf("\nenter the array elements\n");
          for(i=0;i<n;i++){scanf("%d",&a[i]);}
             sort();
   return 0;
          }

int sort()
{
int p=0,rad[20],q,s=0,i=0;
int k1,k2,t; 
while(p<=3)
     {
q=0;
          while(q<=9)
               {
                 i=0;
          while(a[i]!='\0')
               {
                 t=pow(10,(p+1));
                 k1=a[i]%t;
                 k2=k1/pow(10,p);

          if(k2==q) {rad[s]=a[i];s++;}
             i++;
                }
         q++;
                }
 s=0;
 printf("\n sort for %dth\n",p);
 for(i=0;i<n;i++){a[i]=rad[i];}
 printf("\n");
  display();
 p++;
       }
return 0;
       }    

int display(){
int i=0;
while(a[i]!='\0')
 {
printf("%d\t",a[i]);
i++;
 }
return 0;
              }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!