Bubble Sort,bubblesort_PHP教程

來源:互聯網
上載者:User

Bubble Sort,bubblesort


8 numbers. Sort as ascend.

1st loop, compare 7 times (for 8 numbers), and found the largest 8.

2nd loop, compare 6 times (for 7 numbers), and found the largest 7.

. . .

1, 7, 8

2, 6, 7

3, 5, 6

4, 4, 5

5, 3, 4

6, 2, 3

7, 1, 2

In conclusion: For sorting 8 numbers, we need an outer loop of 7 times, each time for finding a largest number; and an inner loop from comparing 7 times to comparing 1 time (as in the center column).

Implementation in PHP:

 1 php 2 /* bubble sort:  3     1. operate directly on the input array (&), not on a copy 4     2. sort as ascend 5  6     a is array 7     m is length of a 8     n is times of outer loop, n-i is times of comparing for each outer loop 9     i/j is for-loop counter10     w is for value swap11 */12 function sortBubble(&$a){13     $m = count($a);14     $n = $m - 1;15     for($i=0; $i<$n; $i++){16         for($j=0; $j<$n-$i; $j++){17             if($a[$j] > $a[$j+1]){18                 $w = $a[$j];19                 $a[$j] = $a[$j+1];20                 $a[$j+1] = $w;21             }22             else{23                 // do nothing24             }25         }26         // see the results after each outer loop27         // echo implode(', ', $a).'
';
28 }29 }30 31 $arr = array(9, 5, 2, 7, 3);32 sortBubble($arr);33 echo implode(', ', $arr);34 35 // 2, 3, 5, 7, 937 ?>


冒泡排序c

冒泡排序詳細注釋:
/* 用冒泡排序法對一維整型數組中的十個數升序排序 */
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[j];/* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;i<10;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0時:
j從0開始a[0],a[1]比較大小,把其中的較大者給a[1],然後j++,a[1]和a[2]再比較,再把兩者中的
較大者給a[2],這樣a[0],a[1],a[2]中的最大者已經交換到a[2]中,這個過程繼續,直到j=10-i-1=9這樣
a[9]中的為10個數中的最大數。
然後i=1時:
由於最大數已找到並放到a[9]中,所以這一次迴圈j最大隻需到10-i-1=8,即a[8]即可,再次從j=0開始a[j]和a[j+1]兩兩比較交換,最後次大數放到a[8]中
然後i++,繼續...
當i=9時已經過9次兩兩比較完成所有排序,i<9不再成立退出比較。
對於n個數,只需要進行n-1次外迴圈的兩兩比較就完成排序。
至於按降序排列只需將if(a[j]>a[j+1])改為if(a[j]
-------------------------------------------------------------------
/* 用改進型冒泡排序法對一維整型數組中的十個數升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 改進型冒泡法排序 */
{ flag=0;
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{ t=a[j]; /* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......餘下全文>>
 

冒泡排序c

冒泡排序詳細注釋:
/* 用冒泡排序法對一維整型數組中的十個數升序排序 */
#include
#include

int main()
{
int i,j,t,a[10];
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 冒泡法排序 */
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{t=a[j];/* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
}
printf("The sequence after sort is:\n");
for(i=0;i<10;i++)
printf("%-5d",a[i]);
printf("\n");
system("pause");
return 0;
}
其中i=0時:
j從0開始a[0],a[1]比較大小,把其中的較大者給a[1],然後j++,a[1]和a[2]再比較,再把兩者中的
較大者給a[2],這樣a[0],a[1],a[2]中的最大者已經交換到a[2]中,這個過程繼續,直到j=10-i-1=9這樣
a[9]中的為10個數中的最大數。
然後i=1時:
由於最大數已找到並放到a[9]中,所以這一次迴圈j最大隻需到10-i-1=8,即a[8]即可,再次從j=0開始a[j]和a[j+1]兩兩比較交換,最後次大數放到a[8]中
然後i++,繼續...
當i=9時已經過9次兩兩比較完成所有排序,i<9不再成立退出比較。
對於n個數,只需要進行n-1次外迴圈的兩兩比較就完成排序。
至於按降序排列只需將if(a[j]>a[j+1])改為if(a[j]
-------------------------------------------------------------------
/* 用改進型冒泡排序法對一維整型數組中的十個數升序排序 */
#include
#include
int main()
{int i,j,t,a[10],flag;
printf("Please input 10 integers:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<9;i++) /* 改進型冒泡法排序 */
{ flag=0;
for(j=0;j<10-i-1;j++)
if(a[j]>a[j+1])
{ t=a[j]; /* 交換a[i]和a[j] */
a[j]=a[j+1];
a[j+1]=t;
flag=1;
}
if(flag==0)break;
}
printf("The sequence after sort is:\n&......餘下全文>>
 

http://www.bkjia.com/PHPjc/908125.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/908125.htmlTechArticleBubble Sort,bubblesort 8 numbers. Sort as ascend. 1st loop, compare 7 times (for 8 numbers), and found the largest 8. 2nd loop, compare 6 times (for 7 numbers), and found the lar...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.