#include<stdio.h>
int main()
{
int arr[] = {9,2,4,10,7,9,11,6,4,8,11,3};
int n = 12;
// copy the input string
int nCpy[n],k=0, count;
for(int i =0; i<n; i++){
nCpy[i] = arr[i];
}
//creates a new array with 0s in each index
int newArr[n];
for(int i = 0; i<n; i++){
newArr[i] =0;
}
// records the frequency of each number
for(int i =0; i<n; i++){
newArr[arr[i]]++;
}
// finds the last index before the first index with a value other than 0
while(newArr[k]== 0){
k++;
}
// records the indexes:
for(int i =0; i<n; i++){
if(newArr[i] != 0){
if(i == k){
count = 0;
newArr[i] = count;
} else{
newArr[i] += count;
count = newArr[i];
}
}
}
// arr is sorted
for(int i=0; i<n; i++){
arr[newArr[nCpy[i]]] = nCpy[i];
newArr[nCpy[i]]--;
}
for(int i =0; i<n; i++){
printf("%d", arr[i]);
}
}
No comments:
Post a Comment