Home > Backend Development > C#.Net Tutorial > How to implement bubble sorting from big to small in C language

How to implement bubble sorting from big to small in C language

coldplay.xixi
Release: 2020-06-08 17:38:31
Original
9813 people have browsed it

How to implement bubble sorting from big to small in C language

#How to implement bubble sorting from large to small in C language?

C language bubble sorting method:

First select the first number as the largest and then compare the numbers in pairs to get the difference between the two. The maximum value between them is compared in sequence. The specific code implementation is as follows:

#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;
}
Copy after login

Recommended tutorial: "C Video Tutorial"

The above is the detailed content of How to implement bubble sorting from big to small in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template