基数排序
代码:
#include<iostream>using namespace std;
void radix(int data[], int size);int main()
{int buff[7] = { 5, 2, 1, 4, 9, 6, 8 };for (int i = 0; i < 7; i++)cout << buff[i];cout << endl;radix(buff, 7);for (int i = 0; i < 7; i++)cout << buff[i];cout << endl;return 0;
}void radix(int data[], int size)
{int n;for (int i = 1; i <= 100; i = i * 10){int tmp[20][10] = { 0 };//建立一个20行,10列的数组,每一列分别代表0~9位数,20行代表能存放的总个数for (int j = 0; j<size; j++){n = (data[j] / i) % 10;tmp[j][n] = data[j];}int k = 0;for (int p = 0; p<10; p++)for (int q = 0; q<size; q++){if (tmp[q][p] != 0)data[k++] = tmp[q][p];}}
}
结果: