c语言冒泡排序的循环写法,C语言冒泡排序三种写法

#include

/*

@function: bubble_sort_one

@functional: bubble sort

@order for parameter value, 1 is ascending and 0 is descending

*/

static int bubble_sort_one(int order)

{

int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};

int i = 0, j = 0;

int temp = 0;

int number_of_times = 0;

int arry_length = sizeof(arry)/sizeof(int);

for(i = 0; i < arry_length; i++)

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

printf("\n");

if (order == 1) { /*ascending order*/

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

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

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

temp = arry[j];

arry[j] = arry[j+1];

arry[j+1] = temp;

}

number_of_times++;

}

}

} else { /*descending order*/

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

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

if (arry[j] < arry[j+1]) {

temp = arry[j];

arry[j] = arry[j+1];

arry[j+1] = temp;

}

number_of_times++;

}

}

}

printf("i = %d,j = %d\n",i,j);

for(i = 0; i < arry_length; i++)

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

printf("\n");

printf("bubble_sort_one: number of times = %d\n\n", number_of_times);

return 0;

}

/*

@function: bubble_sort_two

@functional: bubble sort

@order for parameter value, 1 is ascending and 0 is descending

*/

static int bubble_sort_two(int order)

{

int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};

int i = 0, j = 0;

int temp = 0;

int number_of_times = 0;

int arry_length = sizeof(arry)/sizeof(int);

for(i = 0; i < arry_length; i++)

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

printf("\n");

if (order == 1) { /*ascending order*/

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

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

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

temp = arry[j];

arry[j] = arry[j+1];

arry[j+1] = temp;

}

number_of_times++;

}

}

} else { /*descending order*/

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

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

if (arry[j] < arry[j+1]) {

temp = arry[j];

arry[j] = arry[j+1];

arry[j+1] = temp;

}

number_of_times++;

}

}

}

printf("i = %d,j = %d\n",i,j);

for(i = 0; i < arry_length; i++)

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

printf("\n");

printf("bubble_sort_two: number of times = %d\n\n", number_of_times);

return 0;

}

/*

@function: bubble_sort_three

@functional: bubble sort

@order for parameter value, 1 is ascending and 0 is descending

*/

static int bubble_sort_three(int order)

{

int arry[] = {12, 13, 5, 3, 14, 90, 0, 11, 23, 9, 15, 99, 100, 96};

int i = 0, j = 0;

int temp = 0;

int number_of_times = 0;

int swap_flag = 0;

int arry_length = sizeof(arry)/sizeof(int);

for(i = 0; i < arry_length; i++)

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

printf("\n");

if (order == 1) { /*ascending order*/

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

swap_flag = 0;

for(j = arry_length - 1; j > i; j--) {

if (arry[j - 1] > arry[j]) {

temp = arry[j - 1];

arry[j - 1] = arry[j];

arry[j] = temp;

swap_flag = 1;

}

number_of_times++;

}

if (!swap_flag)

break;

}

} else { /*descending order*/

for(i = 0; i < arry_length - i; i++) {

swap_flag = 0;

for(j = arry_length - 1; j > i; j--) {

if (arry[j-1] < arry[j]) {

temp = arry[j - 1];

arry[j-1] = arry[j];

arry[j] = temp;

swap_flag = 1;

}

number_of_times++;

}

if (!swap_flag)

break;

}

}

printf("i = %d,j = %d\n",i,j);

for(i = 0; i < arry_length; i++)

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

printf("\n");

printf("bubble_sort_three: number of times = %d\n\n", number_of_times);

return 0;

}

int main(void)

{

bubble_sort_one(1);

bubble_sort_two(1);

bubble_sort_three(1);

return 0;

}

gcc sort.c

./a.out

12 13 5 3 14 90 0 11 23 9 15 99 100 96

i = 13,j = 13

0 3 5 9 11 12 13 14 15 23 90 96 99 100

bubble_sort_one: number of times = 169

12 13 5 3 14 90 0 11 23 9 15 99 100 96

i = 13,j = 1

0 3 5 9 11 12 13 14 15 23 90 96 99 100

bubble_sort_two: number of times = 91

12 13 5 3 14 90 0 11 23 9 15 99 100 96

i = 5,j = 5

0 3 5 9 11 12 13 14 15 23 90 96 99 100

bubble_sort_three: number of times = 63

三种冒泡排序写法。第一种,有 n 个数据,则冒泡 n*(n-1)次。第二种,因为每次冒泡,都会确认一个当次最大或最小数值的位置,没必要每次都比较到最后,优化第一种写法,则冒泡 n*(n-1)/2 次。第三种,在第二种的基础上,进一步优化,只要确定了有一次没发生交换,就表明已经排好序了,没必要再进行下一轮的比较。第三种冒泡排序所用的时间,将进一步减少。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值