DihKuh
|
分享:
▲
for (counti = 0; counti < max-1; counti++) //counti=0比11(max-1=12-1)小 for (countj = counti+1; countj < max; countj++) //這裡應該countj = counti+1比較合理 //countj =0+1=1比12(max=12)小 if (number[counti] < number[countj]) //因為counti=0,countj=1所以判斷"number[0] < number[1]" => 判斷"141<491"是否為真 //你的程式counti=0,countj=0變成判斷"141<141"是否為真,這樣與自己比較多做一個比較 { buffer = number[counti]; number[counti] = number[countj]; number[countj] = buffer; } //若為真則執行{}裡的動作,結果number[0]=491,number[1]=141,回第2迴圈控制運算 //number[12] = {491,141,94,19,1078,20,767,9,40,812,538,1024}; //若為假直接回第2迴圈控制運算
接下來就反覆上面省略說明 作指令"countj++" =>countj=1+1=2,這時counti不變counti=0, 再來做判斷比較"number[0] < number[2]" => "491<94"是否為真,若為真責執行{}裡的互換動作,做完回第2迴圈控制運算,若為假則直接回第2迴圈控制運算 因為counti這裡都不變所以我稱為"比較變數"而countj一直換到下一個數字所以我稱為"被比較變數" countj=2---->number[12] = {491,141,94,19,1078,20,767,9,40,812,538,1024}; countj=3---->"491<19"------>number[12] = {491,141,94,19,1078,20,767,9,40,812,538,1024}; countj=4---->"491<1078"--->number[12] = {1078,141,94,19,491,20,767,9,40,812,538,1024}; countj=5---->"1078<20"----->number[12] = {1078,141,94,19,491,20,767,9,40,812,538,1024}; countj=11-->"1078<1024"-->number[12] = {1078,141,94,19,491,20,767,9,40,812,538,1024}; countj=12沒有小於max=12回到第一個迴圈作counti++,這時第1個數字就是最大的 counti=1,countj=2--->"141<94"--->number[12] = {1078,141,94,19,491,20,767,9,40,812,538,1024}; counti=1,countj=3--->"141<19"--->number[12] = {1078,141,94,19,491,20,767,9,40,812,538,1024}; counti=1,countj=4--->"141<491"--->number[12] = {1078,491,94,19,141,20,767,9,40,812,538,1024}; 一直換到第2個數字是第2大的,就這樣循環排列
|