当前位置:网站首页>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]);
}
}
}
}
边栏推荐
- 4、安装部署Spark(Spark on Yarn模式)
- FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
- [yarn] CDP cluster yarn configuration capacity scheduler batch allocation
- 互联网协议详解
- 机器学习--决策树(sklearn)
- Password free login of distributed nodes
- Vert. x: A simple login access demo (simple use of router)
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- Hutool中那些常用的工具类和方法
- Solution of deleting path variable by mistake
猜你喜欢
随机推荐
【kerberos】深入理解kerberos票据生命周期
冒泡排序【C语言】
Using LinkedHashMap to realize the caching of an LRU algorithm
機器學習--線性回歸(sklearn)
Wangeditor rich text reference and table usage
FreeRTOS 任务函数里面的死循环
Come and walk into the JVM
[Blue Bridge Cup 2017 preliminary] buns make up
Machine learning -- decision tree (sklearn)
选择法排序与冒泡法排序【C语言】
ESP8266通过arduino IED连接巴法云(TCP创客云)
Variable star user module
L2-006 tree traversal (25 points)
共用体(union)详解【C语言】
Wangeditor rich text component - copy available
uCOS-III 的特点、任务状态、启动
分布式節點免密登錄
Those commonly used tool classes and methods in hutool
wangeditor富文本引用、表格使用问题
Hutool中那些常用的工具类和方法