登 录
#include <iostream> #define Length 8 #define super 100000 using namespace std; void Counting_Sort(int A[],int B[],int k) { int C[6]={0}; //C[i]包含等于i的元素个数 for(int j=1;j<=Length;++j) { C[A[j]] += 1; } //C[i]包含小于等于i的元素个数 for(int i=1; i<6; ++i) { C[i] = C[i]+C[i-1]; } for(int j=Length; j>0;j--) { B[C[A[j]]] = A[j]; C[A[j]]=C[A[j]] - 1; } } //输出数组中的元素 void printArray(int *A) { for(int i=1; i<=Length;++i) { cout << *(A+i)<<" "; } } int main() { int a[]={super,2,5,3,0,2,3,1,3}; int b[Length]={super,0,0,0,0,0,0,0}; cout << "排序前:"<<endl; printArray(a); Counting_Sort(a,b,5); cout <<endl<< "排序后:"<<endl; printArray(b); return 0; }
抱歉!评论已关闭.