Radix Sort Algorithm

前端 未结 4 1242
时光说笑
时光说笑 2021-01-14 07:39

I have been given some algorithms to reverse engineer. The algorithm below is a radix sort, but I am very confused about what is actually happening in the code.

I\'

相关标签:
4条回答
  • 2021-01-14 07:50

    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.

    0 讨论(0)
  • 2021-01-14 07:56
    /*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;
                  }
    
    0 讨论(0)
  • 2021-01-14 07:58

    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
    
    0 讨论(0)
  • 2021-01-14 08:07

    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.

    0 讨论(0)
提交回复
热议问题