- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
Could you explain me the function of the following two algorithms? (Thinking)
Could you explain me the function of the following two algorithms? (Thinking)
Code:
int countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[n] ;
for (int i=0; i < n; i++)
count[i] = 0;
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%n ]++;
for (i = 1; i < n; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%n] - 1] = arr[i];
count[(arr[i]/exp)%n]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
Code:
void sort(int arr[], int n)
{
countSort(arr, n, 1);
countSort(arr, n, n);
}