代码:
#include "iostream" using namespace std; int counting_sort(int a,int b,int A[],int B[],int k){ int C[10]; int i; for(i=0;i<=k;i++) C[i]=0; for(i=1;i<12;i++){ C[A[i]]++; } for(i=1;i<=k;i++) C[i]=C[i-1]+C[i]; return C[b]-C[a-1]; } void display(int A[]){ int i; for(i=1;i<=11;i++) cout<<A[i]<<" "; cout<<endl; } void main(){ int A[12]={6,0,2,0,1,3,4,6,1,3,2}; int k=6; int B[12]; display(A); cout<<"在区间[1,4]中的个数为:"<<endl; cout<<counting_sort(1,4,A,B,k)<<endl; getchar(); getchar(); }