当前位置:网站首页>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 ->@@
}
}
}
边栏推荐
猜你喜欢
随机推荐
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
Synchronize files using unison
C language replaces spaces in strings with%20
Sqli labs level 1
zipkin 简单使用
C# 高德地图 根据经纬度获取地址
Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
sqli-labs第12关
Don't know mock test yet? An article to familiarize you with mock
一个经典约瑟夫问题的分析与解答
Application of kotlin - higher order function
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
小米电视不能访问电脑共享文件的解决方案
Pclpy projection filter -- projection of point cloud to cylinder
Gateway 简单使用
Network security - summary and thinking of easy-to-use fuzzy tester
一、Qt的核心类QObject
Pointer initialization
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
IP protocol and IP address









