01 bubbleSort

#include<cstdio>
#include <memory>
#include <iostream>
using namespace std;

void bubblesort(int A[],int n)
{
    for(bool sorted=false;sorted=!sorted;n--)
        for(int i=1;i<n;i++)
            if(A[i-1]>A[i])
            {
                swap(A[i-1],A[i]);
                sorted=false;
            }
}
int main()
{
    int a[10]={1,2,3,0,4,5,6,7,8,9};
    bubblesort(a,10);
    for(int i=0;i<10;i++)
        cout<<a[i]<<endl;

}

//起泡排序
/*问题:该算法必然会结束?至少迭代多少趟?
不变性:经过k轮扫描交换后,最大的k个元素必然就位
单调性:经过k轮的扫描交换后,问题的规模缩减至n-k
正确性:经过至多n轮扫描后,算法必然终止,并得出正确答案
*/
原文地址:https://www.cnblogs.com/CheeseIce/p/9763111.html