一、冒泡排序Bubble sort

https://www.cnblogs.com/kkun/archive/2011/11/23/2260312.html#3824357

#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;
}

猜你喜欢

转载自www.cnblogs.com/pengwang52/p/11538409.html
今日推荐