Pages

Related Posts with Thumbnails
Linear Sorts–Counting, Radix–C Programming >> Codes-N-Tricks
Content feed Comments Feed

Linear Sorts–Counting, Radix–C Programming

Posted by Soham Tuesday, May 24, 2011

I have implemented radix and counting sorts in this program where i take inputs as the number of the numbers you want to sort and the number of digits(at max).

Then the numbers, which will be sorted, are taken as random and sorted according to the corresponding algorithm.

Take a look at the C-Code.

Radix and Counting Sort
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5.  
  6. void count_sort(int num[],int sortd[],int no,int rng);
  7. void show(int inp[],int no);
  8. void radixsort(int a[],int n,int d);
  9.  
  10. int cnt_rdx=0,cnt_cntsrt=0;
  11.  
  12. int main()
  13. {
  14.     int no,i,digt,rng;
  15.     int *num,*cnt_srt_num;
  16.     
  17.     printf("\nHow many Numbers you want to sort:- ");
  18.     scanf("%d",&no);
  19.     printf("\nWhat is the Number of Digits(at max) in a number:- ");
  20.     scanf("%d",&digt);
  21.     rng=pow(10,digt);
  22.  
  23.     num=(int *)calloc(no,sizeof(int));
  24.     cnt_srt_num=(int *)calloc(no,sizeof(int));
  25.     srand(time(0));
  26.     for(i=0;i<no;i++)
  27.         num[i]=rand()%rng;
  28.     printf("\nThe Unsorted Numbers are:");
  29.     show(num,no);
  30.     count_sort(num,cnt_srt_num,no,pow(10,digt));
  31.     printf("\nThe Counting Sorted Numbers are:");
  32.     show(cnt_srt_num,no);
  33.     printf("Time Complexity : %d\n",cnt_rdx);
  34.     radixsort(num,no,digt);
  35.     printf("\nThe Radix Sorted Numbers are:");
  36.     show(num,no);
  37.     printf("Time Complexity : %d\n",cnt_cntsrt);
  38.     getch();
  39.     return 0;
  40. }
  41.  
  42. void count_sort(int num[],int sortd[],int no,int rng)
  43. {
  44.     int i;
  45.     int *indx_num;
  46.  
  47.     indx_num=(int *)calloc(rng,sizeof(int));
  48.     for(i=0;i<no;i++)
  49.         indx_num[num[i]]++;
  50.     for(i=1;i<rng;i++)
  51.         indx_num[i]=indx_num[i]+indx_num[i-1];
  52.     for(i=no-1;i>=0;i--)
  53.     {
  54.         cnt_rdx++;
  55.         sortd[indx_num[num[i]]-1]=num[i];
  56.         indx_num[num[i]]--;
  57.     }
  58. }
  59.  
  60. void show(int inp[],int no)
  61. {
  62.     int i;
  63.     for(i=0;i<no;i++)
  64.         printf("  %d",inp[i]);
  65.     printf("\n");
  66. }
  67.  
  68. void radixsort(int a[],int n,int d)
  69. {
  70.     int end[10],start[10],first,p,q,exp,k,i,y,j;
  71.     struct nd
  72.     {
  73.         int info;
  74.         int next;
  75.     };
  76.  
  77.     struct nd *node;
  78.     node=(struct nd *)malloc(n*sizeof(struct nd));
  79.  
  80.  
  81.     for(i=0;i<n-1;i++)
  82.  
  83.     {
  84.         node[i].info=a[i];
  85.         node[i].next=i+1;
  86.     }
  87.     node[n-1].info=a[n-1];
  88.     node[n-1].next=-1;
  89.     first=0;
  90.  
  91.     for(k=1;k<=d;k++)      //consider 3 digit number
  92.     {
  93.         for(i=0;i<10;i++)
  94.         {
  95.             start[i]=-1;
  96.             end[i]=-1;
  97.         }
  98.  
  99.     while(first!=-1)
  100.     {
  101.         cnt_cntsrt++;
  102.         p=first;
  103.         first=node[first].next;
  104.         y=node[p].info;
  105.         exp=pow(10,k-1);
  106.         j=(y/exp)%10;
  107.         q=end[j];
  108.         if(q==-1)
  109.             start[j]=p;
  110.         else
  111.             node[q].next=p;
  112.         end[j]=p;
  113.     }
  114.     for(j=0;j<10&&start[j]==-1;j++)
  115.     ;
  116.     first=start[j];
  117.     while(j<=9)
  118.     {
  119.         for(i=j+1;i<10&&start[i]==-1;i++)
  120.         ;
  121.         if(i<=9)
  122.         {
  123.             p=i;
  124.             node[end[j]].next=start[i];
  125.         }
  126.         j=i;
  127.     }
  128.     node[end[p]].next=-1;
  129. }
  130. //copy into original array
  131. for(i=0;i<n;i++)
  132. {
  133.     a[i]=node[first].info;
  134.     first=node[first].next;
  135. }
  136.  
  137. /*printf("Sorted Data:\n");
  138. for(i=0;i<n;i++)
  139.     printf("%d.  %d\n",i+1,a[i]);*/
  140. }
  141.  
  142. /*void radixsort(int a[],int n)
  143. {
  144. int i,b[MAX],m=0,exp=1;
  145. int bucket[10]={0};
  146. for(i=0;i<n;i++)
  147. {
  148. if(a[i]>m)
  149. m=a[i];
  150. }
  151.  
  152. while(m/exp>0)
  153. {
  154.  
  155. for(i=0;i<n;i++)
  156. bucket[a[i]/exp%10]++;
  157. for(i=1;i<10;i++)
  158. bucket[i]+=bucket[i-1];
  159. for(i=n-1;i>=0;i--)
  160. b[--bucket[a[i]/exp%10]]=a[i];
  161. for(i=0;i<n;i++)
  162. a[i]=b[i];
  163. exp*=10;
  164.  
  165. #ifdef SHOWPASS
  166. printf("\nPASS : ");
  167. print(a,n);
  168. #endif
  169. }
  170. }*/

Discussions will be carried out later,if needed. Any suggestions are highly solicited.

Thank You.

1 Response to Linear Sorts–Counting, Radix–C Programming

  1. Soham Says:
  2. Sorry,but I can't get you...

     

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us