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

c语言冒泡排序的方法:

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

#include <iostream>#include <time.h>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视频教程》

更多相关文章

  1. c语言fgets函数用法是什么?
  2. C语言自定义函数返回数组的方法(图文详解)
  3. c语言puts函数用法是什么?
  4. C语言函数基础知识有哪些?
  5. c语言的阶乘函数如何用递归定义?
  6. C语言中的字符串比较函数是什么
  7. C语言 strchr 函数用法
  8. 一个c源程序中至少应包括一个什么函数
  9. C语言自定义函数(图文详解)

随机推荐

  1. 如何去写 Android(安卓)init.rc (Android
  2. 开启 Android 反射调用
  3. Android MediaController
  4. 通过JS或PHP检测Android
  5. Android在设置里面添加新功能的方法
  6. 关于android:focusable属性
  7. [AndroidTips]Android预定义样式
  8. 基于百度地图API的Android公交换乘导航
  9. Android API Level对应Android版本一览表
  10. Unable to resolve target 'android-5'