一维数组冒泡排序法
1. 作用
将乱序的一维数组按照从小到大的顺序排列
2. 原理示意图
3. 代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>int main(void)
{int a[5] = {0};int len = sizeof(a) / sizeof(a[0]);int i = 0;int j = 0;int tmp = 0;srand(time(NULL));for (i = 0; i < len; i++){a[i] = rand() % 100;}for (i = 0; i < len; i++){printf("%-2d ", a[i]);}putchar('\n');for (j = 0; j < len-1; j++) //排序轮数,每完成一轮排序,本轮中最大的数字被排在最右边{for (i = 0; i < len-1-j; i++) //第j轮排序{if (a[i] > a[i+1]){tmp = a[i];a[i] = a[i+1];a[i+1] = tmp;}}}for (i = 0; i < len; i++){printf("%-2d ", a[i]);}putchar('\n');return 0;
}