一、冒泡排序Bubble sort

我的未来我决定 提交于 2019-11-29 23:20:04

https://www.cnblogs.com/kkun/archive/2011/11/23/bubble_sort.html

 

#include<iostream>
#include <stdio.h> 
#include <stack> 

using namespace std;

void bubble_sort(int *a, int begin, int end) {
    for(int i = begin; i < end-1; i++) 
        for (int j = i+1; j < end; j++) {
            if (a[i] > a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
}

int main()
{
    int a[8] = {4, 2, 6, 7, 9, 5, 1, 3};
    int length = sizeof(a) / sizeof(a[0]);
    
    bubble_sort(a, 0, length);
    for(int i=0; i<8; i++) {
        cout << a[i] << " " << endl;
    }
    return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!