[題目分析]題目要求按B數組內容調整A數組中記錄的次序可以從i=開始檢查是否B[i]=i如是則A[i]恰為正確位置不需再調否則B[i]=k≠i則將A[i]和A[k]對調B[i]和B[k]對調直到B[i]=i為止
void CountSort (rectype A[]int B[])
//A是個記錄的數組B是整型數組本算法利用數組B對A進行計數排序
{int ijn=;
i=;
while(i<n)
{if(B[i]!=i) //若B[i]=i則A[i]正好在自己的位置上則不需要調整
{ j=i;
while (B[j]!=i)
{ k=B[j]; B[j]=B[k]; B[k]=k; // B[j]和B[k]交換
r=A[j];A[j]=A[k]; A[k]=r; } //r是數組A的元素類型A[j]和A[k]交換
i++;} //完成了一個小循環第i個已經安排好
}//算法結束
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23029.html