kehuadong

冒泡排序

#include "common.h"

#define ASIZE(a) (sizeof(a)/sizeof(a[0]))

void bunble_sort(int* a, int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = size -  1; j > i; j--)
        {
            // 下面更小,作为气泡往上浮
            if (a[j] < a[j-1])
            {
                int temp = a[j];
                a[j] = a[j-1];
                a[j-1] = temp;
            }
        }
    }
}

int main()
{
    int a[10];

    rand_array(a, ASIZE(a));
    print_array(a, ASIZE(a));
    
    bunble_sort(a, ASIZE(a));
    print_array(a, ASIZE(a));
}

 

posted on 2024-09-03 10:17  kehuadong  阅读(3)  评论(0编辑  收藏  举报

导航