冒泡排序

冒泡排序

#include<iostream>
using namespace std;

int main()
{
    int r[100];
    int n,exchange,bound;
    cin>>n;
    for(int i=1; i<=n; i++) cin>>r[i];
    exchange=n;
    while(exchange!=0)
    {
        bound=exchange;
        exchange = 0;
        for(int j=1; j<bound; j++)
        {
            if(r[j]>r[j+1])
            {
                int temp;
                temp=r[j];
                r[j]=r[j+1];
                r[j+1]=temp;
                exchange=j;
            }
        }
    }
    for(int i=1; i<=n; i++) cout<<r[i]<<" ";
    return 0;
}

 

posted @ 2017-11-30 14:06  magicalzh  阅读(179)  评论(0)    收藏  举报