Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Radix Sort
- #include<stdio.h>
- #include<math.h>
- #define MAX_SIZE 20
- int arr[MAX_SIZE],n,s0[MAX_SIZE],s1[MAX_SIZE],s2[MAX_SIZE],s3[MAX_SIZE],s4[MAX_SIZE],s5[MAX_SIZE],s6[MAX_SIZE],s7[MAX_SIZE],s8[MAX_SIZE],s9[MAX_SIZE],top[]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},pos=0;
- int findMaxDig()
- {
- int i,max=0,temp,count;
- for(i=0;i<n;i++)
- {
- count=0;
- temp=arr[i];
- while(temp>0)
- {
- count++;
- temp/=10;
- }
- if(count>max)
- max=count;
- }
- return max;
- }
- void push(int s[MAX_SIZE],int t, int ele)
- {
- s[++top[t]]=ele;
- }
- void pop(int s[MAX_SIZE],int t)
- {
- int i;
- for(i=0;i<=top[t];i++)
- {
- arr[pos++]=s[i];
- }
- }
- radixSort(int maxDig)
- {
- int i,j,k,place,extr,dig;
- for(i=1;i<=maxDig;i++) //MaxDig num of passes
- {
- place=pow(10,i);
- extr=pow(10,i-1);
- for(j=0;j<n;j++) //n elements
- {
- dig=arr[j]%place;
- dig/=extr;
- switch(dig)
- {
- case 0:
- push(s0,0,arr[j]);
- break;
- case 1:
- push(s1,1,arr[j]);
- break;
- case 2:
- push(s2,2,arr[j]);
- break;
- case 3:
- push(s3,3,arr[j]);
- break;
- case 4:
- push(s4,4,arr[j]);
- break;
- case 5:
- push(s5,5,arr[j]);
- break;
- case 6:
- push(s6,6,arr[j]);
- break;
- case 7:
- push(s7,7,arr[j]);
- break;
- case 8:
- push(s8,8,arr[j]);
- break;
- case 9:
- push(s9,9,arr[j]);
- break;
- }
- }
- pop(s0,0);
- pop(s1,1);
- pop(s2,2);
- pop(s3,3);
- pop(s4,4);
- pop(s5,5);
- pop(s6,6);
- pop(s7,7);
- pop(s8,8);
- pop(s9,9);
- for(k=0;k<10;k++)
- top[k]=-1;
- pos=0;
- }
- }
- void main()
- {
- int i;
- printf("Enter the number of elements:");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- scanf("%d",&arr[i]);
- radixSort(findMaxDig());
- for(i=0;i<n;i++)
- {
- printf("%d\t",arr[i]);
- }
- }
Add Comment
Please, Sign In to add comment