当前位置:网站首页>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]);
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
There are three iPhone se 2022 models in the Eurasian Economic Commission database
SQL时间注入
Basic use of pytest
E-commerce data analysis -- User Behavior Analysis
小天才电话手表 Z3工作原理
TypeScript
4. Install and deploy spark (spark on Yan mode)
Gallery's image browsing and component learning
物联网系统框架学习
Connexion sans mot de passe du noeud distribué
Stage 4 MySQL database
[mrctf2020] dolls
【kerberos】深入理解kerberos票据生命周期
Some concepts often asked in database interview
5G工作原理详解(解释&图解)
Codeforces Round #771 (Div. 2)
MySQL主从复制的原理以及实现
【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
Pytorch实现简单线性回归Demo