编辑代码

#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
#define N 10
#define M 100
#define SWAP(x, y) {int tmp=x; x=y; y=tmp;}

void print(int *a)
{
    for(int i=0; i<N; ++i)
    {
        printf("%3d", a[i]);
    }
    printf("\n");
}

void bubbleSort(int *a)
{
    //N-1 次冒泡
    for(int i=0; i<N-1; ++i)
    {
        //每次冒泡 比较 N-i-1 次
        for(int j=0; j<N-i-1; ++j)
        {
            if(a[j] > a[j+1])
            {
                SWAP(a[j], a[j+1]);
            }
        }
    }
}
int main() {
    srand(time(NULL));
    int *a = (int*)malloc(sizeof(int) * N);
    for(int i=0; i<N; ++i)
    {
        a[i] = rand() % M;
    }

    print(a);

    bubbleSort(a);

    print(a);

    free(a);
    a = NULL;
	return 0;
}