#include <iostream>
#pragma comment(linker, "/STACK:8000000")
#include <ctime>
#include <iomanip>
#include <stdio.h>
using namespace std;
void radixSort (int [], int);
int maxNumber ( int[], int);
void countSort(int [], int , int );
int main(){
const int size = 8;
int arr [] = {110, 5, 10,3 ,22, 100, 1, 23};
//int size = sizeof(arr)/sizeof(arr[0]);
for ( int i = 0; i <size; i++){
cout << setw(7) <<arr[i] ;
}
for ( int i = 0; i < size; i++){
cout << setw(7) <<arr[i] ;
}
cout << endl;
radixSort (arr, size);
system("pause");
return 0;
}
void radixSort ( int arr[], int size){
int exp, m;
m = maxNumber(arr, size);
for (exp = 1; m/exp > 0; exp *= 10)
countSort(arr, size, exp);
}
int maxNumber ( int arr[], int size){
int max = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
void countSort(int arr[], int size , int exp)
{
**int output[size];**
int i, count[10] = {0};
for (i = 0; i < size; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i-1];
for (i = size - 1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < size; i++)
arr[i] = output[i];
}
答案 0 :(得分:1)
你不能在编译时声明一个大小未知的数组,如下所示:
int output[size];
你需要像这样动态声明它:
int* output = new int[size];
请注意,您现在负责清理您在此处分配的内存,并在完成后将其删除:
delete[] output;
或者更好的是使用标准的c ++容器,如std :: vector