linux运行c语言冒泡排序,冒泡排序算法实例程序(C语言)

冒泡排序算法实例程序,C语言代码实现如下:

#include

#include

#define MAX 10

int list[MAX] = {1,8,4,6,0,3,5,2,7,9};

void display(){

int i;

printf("[");

// navigate through all items

for(i = 0; i < MAX; i++){

printf("%d ",list[i]);

}

printf("]\n");

}

void bubbleSort() {

int temp;

int i,j;

bool swapped = false;

// loop through all numbers

for(i = 0; i < MAX-1; i++) {

swapped = false;

// loop through numbers falling ahead

for(j = 0; j < MAX-1-i; j++) {

printf(" Items compared: [ %d, %d ] ", list[j],list[j+1]);

// check if next number is lesser than current no

// swap the numbers.

// (Bubble up the highest number)

if(list[j] > list[j+1]) {

temp = list[j];

list[j] = list[j+1];

list[j+1] = temp;

swapped = true;

printf(" => swapped [%d, %d]\n",list[j],list[j+1]);

}

else {

printf(" => not swapped\n");

}

}

// if no number was swapped that means

// array is sorted now, break the loop.

if(!swapped) {

break;

}

printf("Iteration %d#: ",(i+1));

display();

}

}

main(){

printf("Input Array: ");

display();

printf("\n");

bubbleSort();

printf("\nOutput Array: ");

display();

}

如果我们编译并运行上述程序,那么它应该产生以下结果 -

Input Array: [1 8 4 6 0 3 5 2 7 9 ]

Items compared: [ 1, 8 ] => not swapped

Items compared: [ 8, 4 ] => swapped [4, 8]

Items compared: [ 8, 6 ] => swapped [6, 8]

Items compared: [ 8, 0 ] => swapped [0, 8]

Items compared: [ 8, 3 ] => swapped [3, 8]

Items compared: [ 8, 5 ] => swapped [5, 8]

Items compared: [ 8, 2 ] => swapped [2, 8]

Items compared: [ 8, 7 ] => swapped [7, 8]

Items compared: [ 8, 9 ] => not swapped

Iteration 1#: [1 4 6 0 3 5 2 7 8 9 ]

Items compared: [ 1, 4 ] => not swapped

Items compared: [ 4, 6 ] => not swapped

Items compared: [ 6, 0 ] => swapped [0, 6]

Items compared: [ 6, 3 ] => swapped [3, 6]

Items compared: [ 6, 5 ] => swapped [5, 6]

Items compared: [ 6, 2 ] => swapped [2, 6]

Items compared: [ 6, 7 ] => not swapped

Items compared: [ 7, 8 ] => not swapped

Iteration 2#: [1 4 0 3 5 2 6 7 8 9 ]

Items compared: [ 1, 4 ] => not swapped

Items compared: [ 4, 0 ] => swapped [0, 4]

Items compared: [ 4, 3 ] => swapped [3, 4]

Items compared: [ 4, 5 ] => not swapped

Items compared: [ 5, 2 ] => swapped [2, 5]

Items compared: [ 5, 6 ] => not swapped

Items compared: [ 6, 7 ] => not swapped

Iteration 3#: [1 0 3 4 2 5 6 7 8 9 ]

Items compared: [ 1, 0 ] => swapped [0, 1]

Items compared: [ 1, 3 ] => not swapped

Items compared: [ 3, 4 ] => not swapped

Items compared: [ 4, 2 ] => swapped [2, 4]

Items compared: [ 4, 5 ] => not swapped

Items compared: [ 5, 6 ] => not swapped

Iteration 4#: [0 1 3 2 4 5 6 7 8 9 ]

Items compared: [ 0, 1 ] => not swapped

Items compared: [ 1, 3 ] => not swapped

Items compared: [ 3, 2 ] => swapped [2, 3]

Items compared: [ 3, 4 ] => not swapped

Items compared: [ 4, 5 ] => not swapped

Iteration 5#: [0 1 2 3 4 5 6 7 8 9 ]

Items compared: [ 0, 1 ] => not swapped

Items compared: [ 1, 2 ] => not swapped

Items compared: [ 2, 3 ] => not swapped

Items compared: [ 3, 4 ] => not swapped

Output Array: [0 1 2 3 4 5 6 7 8 9 ]

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值