//基数排序,对每一位的排序采用的插入排序(可改为快速排序或计数排序效果更好)
#include<iostream>
#include<math.h>
using namespace std;
void InsertSort( int* array_A, int* array_B, int n, int n_rhs )
{
for( int i = 0; i < n; i++ )
{
array_B[i] = array_A[i]/((int)pow(10.0, n_rhs-1))%10;
}
for( int j = 1; j < n; j++ )
{
int k = j - 1;
int temp_A = array_A[j];
int temp_B = array_B[j];
while( k >= 0 && array_B[k] > temp_B )
{
array_B[k+1] = array_B[k];
array_A[k+1] = array_A[k];
k--;
}
array_A[k+1] = temp_A;
array_B[k+1] = temp_B;
}
}
void OutPut( int* array, int n)
{
for( int i = 0 ; i < n; i++)
cout<<array[i]<<" ";
cout<<endl;
}
void RadixSort( int* array, int n, int length )
{
int array_temp[n];
for( int i = 1; i <= length; i++ )
{
InsertSort( array, array_temp, n, i);
OutPut( array, n);
}
}
int main()
{
int array[7]={329, 457, 657, 839, 436, 720, 355};
RadixSort( array, 7, 3);
return 0;
}
转载于:https://blog.51cto.com/8734514/1626109