c语言字符起泡法从大到小,c语言冒泡排序怎样实现从大到小

c语言冒泡排序怎样实现从大到小

483bd38d412e7dac02cef8e7a334cc9a.png

c语言冒泡排序怎样实现从大到小?

c语言冒泡排序的方法:

先选定第一个数字为最大再对数字两两进行比较,得到两者之间的最大值,依次比较。具体代码实现如下:#include

#include

using namespace std;

void srandData(int *, int );//产生随机数的函数

void bubbleSort(int *, int );//冒泡排序具体实现函数

void swap(int *, int *);//两个数字实现交换的函数

void display(int *, int );//在屏幕输出结果函数

int main()

{

const int N = 10;//定义常数

int arr[N];//定义数组

srandData(arr, N);

bubbleSort(arr, N);

display(arr, N);

return 0;

}

void srandData(int *a, int n)

{

srand(time(NULL));

for(int i = 0; i < n; i++)

{

a[i] = rand() % 50;//取50以下的数字

cout << a[i] << " ";

}

cout << endl;

}

void swap(int *b, int *c)

{

int temp = *c;

*c = *b;

*b = temp;

}

void bubbleSort(int *a, int n)

{

for(int i = 0; i < n; i++)

{

for(int j = 0; j < n - i - 1; j++)

{

if(a[j] < a[j + 1])

{

swap(&a[j], &a[j + 1]);//两者交换

}

}

}

}

void display(int *d, int n)

{

for(int i = 0; i < n; i++)

{

cout << d[i] << " ";

}

cout << endl;

}

推荐教程: 《C视频教程》

c语言冒泡排序怎样实现从大到小的教程已介绍完毕,更多请关注跳墙网其他文章教程!

c语言冒泡排序怎样实现从大到小相关教程

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值