C++实现基数排序

#include 
using namespace std;

int output_index[] = { 0,0,0,0,0,0,0,0,0,0 };

void init_output_index() {
	for (int i = 0; i < 10; i++) {
		output_index[i] = 0;
	}
}

int** map(int input[], int num,int _) {
	int** output = new int*[10];
	for (int i = 0; i < 10; i++) {
		output[i] = new int[num];
	}
	init_output_index();
	for (int i = 0; i < num; i++) {
		int tmp = input[i]%_*10/_;
		output[tmp][output_index[tmp]] = input[i];
		output_index[tmp]++;
	}
	return output;
}

int* reduce(int** input, int num) {
	int* output = new int[num];
	int index = 0;
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j  max) {
			max = input[i];
		}
	}
	while (max/_ > 0) {
		_ *= 10;
	}
	return _;
}

int main() {
	int num;
	cin >> num;
	int* input=new int[num];
	for (int i = 0; i < num; i++) {
		cin >> input[i];
	}
	int max_=arr_max_(input, num);
	for (int _ = 10; _ <= max_; _ *= 10) {
		input = reduce(map(input, num, _), num);
		for (int i = 0; i < num; i++) {
			cout << input[i] << " ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(c++,数据结构)