Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void CountingSort(int n, int arr[], int sortArr[]);
- void RadixSort(int arr[],int num);
- void print(int *a, int n) {
- int i;
- for (i = 0; i < n; i++)
- printf("%d ", a[i]);
- }
- int main(int argc,char *argv[])
- {
- int type=0;
- if(argc==1){
- printf("Select size of array with numbers starting from 1\n");
- printf("Select sorting type\n c : Counting Sort\n r : Radix Sort\n");
- exit(0);
- }
- else if (argc==2){
- printf("Two arguments needed\n");
- exit(0);
- }
- else if (argc==3){
- if(strstr(argv[2],"c")!=0){
- printf("Selected type: Counting Sort\n");
- type=1;
- }
- else if(strstr(argv[2],"r")!=0){
- printf("Selected type: Radix Sort\n");
- type=2;
- }
- }
- int num,i;
- int *arr,*sortArr;
- num=atoi(argv[1]);
- arr = (int *)malloc(num * sizeof(int));
- sortArr = (int *)malloc(num * sizeof(int));
- for(i=0;i<num;i++){
- scanf("%d", &arr[i]);
- }
- if(type==1){
- CountingSort(num,arr,sortArr);
- print(sortArr,num);
- }
- if(type==2){
- RadixSort(arr,num);
- print(arr,num);
- }
- }
- void CountingSort(int num, int arr[], int sortArr[])
- {
- int k;
- for(int i=0;i<num;i++)
- {
- k=0;
- for(int j=0;j<num;j++)
- {
- if(arr[i]>arr[j])
- k++;
- }
- sortArr[k] = arr[i];
- }
- }
- void RadixSort(int arr[],int num ){
- int bucket[10][5],buck[10],b[10];
- int i,j,k,l,n=0,div=1,large,passes;
- large=arr[0];
- for(i=0;i<num;i++)
- {
- if(arr[i]>large){
- large=arr[i];
- }
- while(large>0){
- n++;
- large=large/10;
- }
- for(passes=0;passes<n;passes++){
- for(k=0;k<10;k++){
- buck[k]=0;
- }
- for(i=0;i<num;i++){
- l=((arr[i]/div)%10);
- bucket[l][buck[l]++]=arr[i];
- }
- i=0;
- for(k=0;k<10;k++){
- for(j=0;j<buck[k];j++)
- {
- arr[i++]=bucket[k][j];
- }
- }
- div*=10;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement