冒泡排序法是一種較簡單的數值大小排序的算法。主要邏輯是:
對于原始的一組線性資料,從頭到尾依次兩兩比較,如果前者大于後者(或者相反)則交換兩個數的位置。這樣一遍做下來,數組最末端即是所有數中最大的(或最小的),接下來将除了最後一個數以外剩下的資料再進行一遍剛才的算法。以此類推,每一遍的資料都越來越少,直到最後隻剩一個時結束,此時排序完成。
Bubble Sort is a simple algorithm ofsequencing a pile of
data, especially numerical numbers. The main method is:
In terms of a set of linear data, compare every two data
from the beginning to the end, if the former one is greater than
the latter one (or vice versa), swap the two’s position. Now the
biggest (or smallest) of all is at the end of the array, then
operate the remaining data (except the last one) using the same way
as before. By analogy, and each time the data we work on is less
and less, stop until there is only one data. Now all things have
completed.

Void bubble_sort(int a[],int n)
{
Int
i,j,temp;
for
(j=0;j
for(i=0;i
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}