Shishu

counting sort in c

Oct 5th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int a[50],b[50],c[50];
  5.     int k=0,i,n,j;
  6.     printf("Enter your array number:\n");
  7.     scanf("%d",&n);
  8.  
  9.     printf("Enter yor data number :");
  10.     for(i=1; i<n; i++)
  11.     {
  12.         scanf("%d",&a[i]);
  13.     }
  14.  
  15.     for(i=1; i<=n; i++)
  16.     {
  17.         if(a[i]>k)
  18.         {
  19.             k=a[i];
  20.         }
  21.     }
  22.     for(i=1; i<=k; i++)
  23.         c[i]=0;
  24.  
  25.     for(j=1; j<=n; j++)
  26.         c[a[j]]+=1;
  27.        
  28.     for(i=2; i<=k; i++)
  29.         c[i]=c[i]+c[i-1];
  30.        
  31.     for(j=n; j>=1; j--)
  32.         b[c[a[j]]]=a[j];
  33.     c[a[j]]-=1;
  34.    
  35.     printf("the sorted number is:");
  36.     for(i=1; i<=n; i++)
  37.     {
  38.         printf("%d ,",b[i]);
  39.     }
  40.  
  41.  
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment