当前位置:网站首页>Select sort and insert sort
Select sort and insert sort
2022-07-02 08:48:00 【Code pirate captain】
1. Selection sort
1) Sort by selection from large to small , From 0 The number begins ;
2) Find the angle sign of the maximum value max, The value in the corresponding array a[max];
3) Take what you get a[max] And a[0] In exchange for ;
4) From 1 The number begins , repeat 2)3);
5) end .
void selectMax(int a[], int n)
{
int max;
int temp;
for (int j = 0; j < n - 1; j++)
{
max = j;
for (int i = j + 1; i < n; i++)
{
if (a[max] < a[i])
{
max = i;// Traverse , Get the subscript of the maximum value in the array
}
}
if (max != j) // Put the maximum value first
{
temp = a[max];
a[max] = a[j];
a[j] = temp;
}
}
}
2. Insertion sort
void InsertSort(int a[], int len)
{
int temp, i, j;
for (i = 1; i < len; i++)
{
if (a[i] < a[i - 1])
{
temp = a[i]; // Save it with a temporary variable
for (j = i - 1; a[j] > temp&& j >= 0; j--)
{
a[j + 1] = a[j]; // Compare everything i The big one will move back , Because big numbers are always behind
}
a[j + 1] = temp; // What needs to be noted here is j+1, transfer bug So tired ->@@
}
}
}
边栏推荐
- HCIA - data link layer
- C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
- Makefile基本原理
- File upload Labs
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
- 队列的基本概念介绍以及典型应用示例
- Qt的connect函数和disconnect函数
- Viewing JS array through V8
- Minecraft module service opening
- Hcia - Application Layer
猜你喜欢
随机推荐
Minecraft空岛服开服
DWORD ptr[]
Openfeign facile à utiliser
Openshift container platform community okd 4.10.0 deployment
Minecraft plug-in service opening
用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
Call Stack
Openshift build image
Installing Oracle database 19C for Linux
一个经典约瑟夫问题的分析与解答
gocv边界填充
Mutex
IP协议与IP地址
Oracle 相关统计
Dip1000 runaway
c语言将字符串中的空格替换成%20
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
What is SQL injection
The best blog to explain the basics of compilation (share)
HCIA—数据链路层