這篇文章將會講解如何用c++實現氣泡排序演算法。儘管stl庫中已提供了排序函式,但是理解如何通過簡單的迴圈實現氣泡排序演算法還是有必要的。
對於氣泡排序演算法更簡潔的理解,可訪問**
#include
#include
using
namespace std;
intmain
(void)}
}//print the result.
cout << endl <<
"from min to max:"
<< endl;
for(
int i =
0; i < serial.
size()
; i++
) cout << serial[i]
<<
" ";
cout << endl;
return0;
}
氣泡排序的簡單實現
冒泡的意思就是一次迴圈中依次比較相鄰位置的數,滿足則交換。第乙個for代表次數,第二個for來真正的兩兩比較。冒泡的關鍵在於迴圈條件的次數確定,先看第乙個for迴圈是length 1,比如5個數 5 3 1 2 0,那麼看第一次比較後變成3 1 2 0 5 第二次變成 1 20 3 5 第三次變成 ...
C 實現氣泡排序
include using namespace std define array size 8 the array size int main cout show the array void myshow int a,int length for unsigned int i 0 i執行結果 演算...
c 實現氣泡排序
氣泡排序 依次比較相鄰的資料,將小資料放在前,大資料放在後 即第一趟先比較第1個和第2個數,大數在後,小數在前,再比較第2個數與第3個數,大數在後,小數在前,以此類推則將最大的數 滾動 到最後乙個位置 第二趟則將次大的數滾動到倒數第二個位置.第n 1 n為無序資料的個數 趟即能完成排序。對氣泡排序演...