冒泡排序

来源:互联网 发布:itsm java 编辑:程序博客网 时间:2024/06/04 21:16
#include <iostream>using namespace std;void swap(int *a, int *b){    int *temp;    temp = a;    a = b;    b = temp;}void  BobbleSort(int a[], int n)          //冒泡排序{    int i, j, flag;    for (i = 0; i <n - 1; ++i)    {        flag = 0;        for (j = n - 1; j>i; j--)        {            if (a[j - 1]>a[j])            {                swap(a[j - 1], a[j]);                flag = 1;            }        }        if (flag == 0)            return;    }}int main(int argc, char const *argv[]){    int a[8] = { 9,4,1,12,43,2,8,10 };    int i;    BobbleSort(a, 8);    for (i = 0; i<8; ++i)    {        cout << a[i] <<" ";    }    cout<<endl;    system("pause"); //在vs上运行,需要加,不然会运行,之后闪退    return 0;}

运行结果

原创粉丝点击