冒泡排序法

#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;
}
step by step.
原文地址:https://www.cnblogs.com/answer727/p/6910575.html