当前位置:网站首页>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]);
}
}
}
}
边栏推荐
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- inline详细讲解【C语言】
- sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- 2019腾讯暑期实习生正式笔试
- XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
- [BSidesCF_2020]Had_a_bad_day
- Common regular expression collation
- RT-Thread 线程的时间片轮询调度
- Yarn installation and use
猜你喜欢

MySQL主从复制的原理以及实现

【flink】flink学习

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries

Togglebutton realizes the effect of switching lights

sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer

Password free login of distributed nodes

快来走进JVM吧

高通&MTK&麒麟 手机平台USB3.0方案对比

Mysql database interview questions

2019 Tencent summer intern formal written examination
随机推荐
SQL时间注入
【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
Double to int precision loss
Machine learning -- linear regression (sklearn)
Mall project -- day09 -- order module
Pytoch Foundation
Pytorch-温度预测
inline详细讲解【C语言】
OPPO VOOC快充电路和协议
【yarn】Yarn container 日志清理
{one week summary} take you into the ocean of JS knowledge
锂电池基础知识
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
快来走进JVM吧
荣耀Magic 3Pro 充电架构分析
Togglebutton realizes the effect of switching lights
Hutool中那些常用的工具类和方法
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
Yarn installation and use
Correspondence between STM32 model and contex M