当前位置:网站首页>Selective sorting and bubble sorting [C language]
Selective sorting and bubble sorting [C language]
2022-07-06 11:57:00 【Weiyuan escort agency】
1、 Sorting by selection
/*
Sorting by selection : From small to large
Here's how it works : First, find the minimum in the unsorted sequence ( Big ) Elements , To the beginning of the collating sequence ,
then , Continue to find the smallest from the remaining unsorted elements ( Big ) Elements , Then put it in the row
End of sequence . And so on , Until all the elements are sorted .
*/
void sel_sort_s2b(int arr[], int n)
{
int min;
int temp;
for (int i = 0; i<(n - 1); i++)
{
min = i;
for (int j = i + 1; j<n; j++)
{
if (arr[min] > arr[j])
{
min = j; // Record the subscript of the lowest value in the array
}
}
if (min != i) //min be equal to i There is no need to exchange
{
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
/*
Sorting by selection : From big to small
Here's how it works : First, find the largest... In the unordered sequence ( Small ) Elements , To the beginning of the collating sequence ,
then , Then continue to find the maximum from the remaining unordered elements ( Small ) Elements , Then put it in the row
End of sequence . And so on , Until all the elements are sorted .
*/
void sel_sort_b2s(int a[], int len)
{
#define SWAP(X,Y) X=X+Y;Y=X-Y;X=X-Y
int max;
for (int i = 0; i < len - 1; i++)
{
max = i;
for (int j = i + 1; j < len; j++)
{
if (a[j] > a[max])
{
max = j;
}
}
if (max != i)
{
SWAP(a[i], a[max]);
}
}
}2、 Bubble sort
/*
Bubble sort : From small to large
Here's how it works : It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they are in order ( : from large to small 、 First letter from A To Z) Mistakes are exchanged .
*/
void bubble_sort_s2b(int arr[], int len)
{
#define SWAP(X,Y) X=X+Y;Y=X-Y;X=X-Y
for (int i = 0; i<(len - 1); i++)
{
for (int j = 0; j<(len - i - 1); j++)
{
if (arr[j] > arr[j + 1])
{
SWAP(arr[j], arr[j + 1]);
}
}
}
}
/*
Bubble sort : From big to small
Here's how it works : It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they are in order ( : from large to small 、 First letter from A To Z) Mistakes are exchanged .
*/
void bubble_sort_b2s(int arr[], int len)
{
#define SWAP(X,Y) X=X+Y;Y=X-Y;X=X-Y
for (int i = 0; i<(len - 1); i++)
{
for (int j = 0; j<(len - i - 1); j++)
{
if (arr[j] < arr[j + 1])
{
SWAP(arr[j], arr[j + 1]);
}
}
}
}
边栏推荐
- PyTorch四种常用优化器测试
- 【kerberos】深入理解kerberos票据生命周期
- Detailed explanation of nodejs
- sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
- 数据分析之缺失值填充(重点讲解多重插值法Miceforest)
- STM32型号与Contex m对应关系
- Encodermappreduce notes
- Small L's test paper
- Vert. x: A simple login access demo (simple use of router)
- [Kerberos] deeply understand the Kerberos ticket life cycle
猜你喜欢

mysql实现读写分离

Password free login of distributed nodes

Mysql database interview questions

机器学习--线性回归(sklearn)

锂电池基础知识

Mysql的索引实现之B树和B+树
![[yarn] CDP cluster yarn configuration capacity scheduler batch allocation](/img/85/0121478f8fc427d1200c5f060d5255.png)
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation

数据分析之缺失值填充(重点讲解多重插值法Miceforest)

Composition des mots (sous - total)

【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
随机推荐
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Wangeditor rich text reference and table usage
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
[yarn] yarn container log cleaning
Pytorch实现简单线性回归Demo
荣耀Magic 3Pro 充电架构分析
RT-Thread 线程的时间片轮询调度
2019腾讯暑期实习生正式笔试
[Flink] cdh/cdp Flink on Yan log configuration
Distribute wxWidgets application
互聯網協議詳解
Small L's test paper
Composition des mots (sous - total)
Implementation scheme of distributed transaction
分布式节点免密登录
5G工作原理详解(解释&图解)
第4阶段 Mysql数据库
[MRCTF2020]套娃
IOT system framework learning