标签:
原理:冒泡排序的原理是比较相邻的两个数据,将较小的数据浮到前面,每一次都把最小的数字浮到最前面,n次遍历之后,记得到已排序结果。
运行过程:
原始数据: 3 5 2 4 10 7 9 11
3 5 2 4 7 10 9 11
3 2 5 4 7 10 9 11
3 2 5 4 7 9 10 11
3 2 4 5 7 9 10 11
2 3 4 5 7 9 10 11
算法复杂度:O(n^2)
C++源代码:
#include <iostream> using namespace std; void bubble_sort(int *a,int n){ int temp; for(int i=0;i<n-1;i++){ for(int j=n-1;j>i;j--){ if(a[j-1]>a[j]){ temp = a[j-1]; a[j-1] = a[j]; a[j] = temp; } } } } int main(){ int a[]={3,5,2,4,10,7,9,11}; bubble_sort(a,8); for(int i=0;i<8;i++){ cout<<a[i]<<" "; } }
标签:
原文地址:http://www.cnblogs.com/heee/p/4550229.html