标签:
从左向右扫描数据,选择最大的数据,放在右边。
要点:比较相邻的两个数,如果左边的数大于右边的数就进行交换。
#include <iostream> using namespace std; void BubbleSort(int list[], int n); int main() { int a[] = {9,8,7,6,5,4,3,2,1}; BubbleSort(a, 9); for(int i = 0; i < 9; i++) cout << a[i] << endl; system("pause"); return 0; } void BubbleSort(int list[], int n) { for(int i = 0; i < n-1; i++) { for(int j = 0; j < n - i -1; j++) { if(list[j] > list[j+1]) std::swap(list[j],list[j+1]); } } }
标签:
原文地址:http://www.cnblogs.com/gongyan/p/4325103.html