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大的,就这样循环排列
|