算法——冒泡排序
代码如下:
1#include <stdio.h>
2#define N 10
3int main(void)
4{
5 int a[N] = {5, 1, 8, 4, 0, 2, 9, 3, 6, 7};
6 int i, flag, temp, j = N;
7 do {
8 flag = 0;
9 for (i = 1; i < j; i++)
10 {
11 if (a[i - 1] > a[i])
12 {
13 flag = 1;
14 temp = a[i - 1];
15 a[i - 1] = a[i];
16 a[i] = temp;
17 }
18 }
19 j--;
20 } while (flag);
21 for (i = 0; i < N; i++)
22 {
23 printf("%5d", a[i]);
24 }
25 printf("n");
26}
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/bubblesort/586.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.