冒泡排序法

#include <iostream>
using namespace std;
//上下为验证,此处为核心。
void bubbleSort(int a[], int N)
{
    for(int i = 0; i < N-1; i++)
    {
        for(int j = 0; j < N - i - 1; j++)
        {
            if(a[j] > a[j+1])
            {
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
    }

}
int main()
{
    int a[5]={7,4,2,5,3};
    bubbleSort(a,5);
    for(int i=0;i<5;i++)
        cout<<a[i]<<endl;
    system("pause");
    return 0;
}

 

posted on 2017-05-26 21:46  那年月光  阅读(107)  评论(0编辑  收藏  举报