当前位置:网站首页>选择法排序与冒泡法排序【C语言】
选择法排序与冒泡法排序【C语言】
2022-07-06 09:16:00 【薇远镖局】
1、选择法排序
/*
选择法排序:由小到大
工作原理如下:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,
然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排
序序列的末尾。以此类推,直到所有元素均排序完毕。
*/
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; //将数组中最小值的下标记录下来
}
}
if (min != i) //min等于i的话不需要交换
{
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
/*
选择法排序:由大到小
工作原理如下:首先在未排序序列中找到最大(小)元素,存放到排序序列的起始位置,
然后,再从剩余未排序元素中继续寻找最大(小)元素,然后放到已排
序序列的末尾。以此类推,直到所有元素均排序完毕。
*/
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、冒泡法排序
/*
冒泡法排序:由小到大
工作原理如下:它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。
*/
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]);
}
}
}
}
/*
冒泡法排序:由大到小
工作原理如下:它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。
*/
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]);
}
}
}
}
边栏推荐
猜你喜欢

MTCNN人脸检测

第4阶段 Mysql数据库

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
![[yarn] CDP cluster yarn configuration capacity scheduler batch allocation](/img/85/0121478f8fc427d1200c5f060d5255.png)
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation
Reading BMP file with C language

分布式節點免密登錄

【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题

B tree and b+ tree of MySQL index implementation

MongoDB

4、安装部署Spark(Spark on Yarn模式)
随机推荐
Composition des mots (sous - total)
L2-004 is this a binary search tree? (25 points)
Niuke novice monthly race 40
SQL时间注入
Pytoch Foundation
[template] KMP string matching
Password free login of distributed nodes
Heating data in data lake?
Pytorch-温度预测
機器學習--線性回歸(sklearn)
Encodermappreduce notes
IOT system framework learning
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
Vs2019 use wizard to generate an MFC Application
Case analysis of data inconsistency caused by Pt OSC table change
Solution of deleting path variable by mistake
电商数据分析--薪资预测(线性回归)
{one week summary} take you into the ocean of JS knowledge
Library function -- (continuous update)
List and set