当前位置:网站首页>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]);
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
E-commerce data analysis -- User Behavior Analysis
Word排版(小计)
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
[Bluebridge cup 2021 preliminary] weight weighing
【yarn】CDP集群 Yarn配置capacity调度器批量分配
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
数据库面试常问的一些概念
Redis interview questions
Password free login of distributed nodes
MySQL START SLAVE Syntax
Linux Yum install MySQL
【CDH】CDH/CDP 环境修改 cloudera manager默认端口7180
XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
Machine learning -- decision tree (sklearn)
[Kerberos] deeply understand the Kerberos ticket life cycle
分布式節點免密登錄
Come and walk into the JVM
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
nodejs 详解