We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Can I do better than this in C?
It passes all testcases, the last one in 0.38s, but I would like to know if there's a better way of doing it.
Any suggestions would be appreciated.
intmain(){intn=0;scanf("%d",&n);int*arr=(int*)calloc(100,sizeof(int));//For implementing counting sortint*input=(int*)calloc(n,sizeof(int));//For storing the input integerschar**strings=(char**)calloc(n,sizeof(char*));//For storing the input stringsfor(inti=0;i<n;i++)strings[i]=(char*)calloc(10,sizeof(char));intnum=0;for(inti=0;i<n;i++){scanf("%d",&num);input[i]=num;arr[num]++;scanf("%s",strings[i]);}for(inti=0;i<100;i++){intj=0;while(arr[i]>0){while(input[j]!=i)//Find the number in input list j++;if(j<n/2)printf("- ");elseprintf("%s ",strings[j]);//print corresponding stringj++;//Increment j to find other instances further ahead in inputarr[i]--;//Decrement count }}return0;}
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
Can I do better than this in C? It passes all testcases, the last one in 0.38s, but I would like to know if there's a better way of doing it. Any suggestions would be appreciated.