当前位置:网站首页>Insert sort, select sort, bubble sort
Insert sort, select sort, bubble sort
2022-07-04 22:51:00 【Winter snow is like spring】
1. Insertion sort
The understanding principle of colloquialism : Think of the first number as ordered , Start with the second number and compare with the number before it ,( The default order is from small to large ) If this number is smaller than the previous one , Then take this number , In a temporary variable , Do the same thing , Knowing that the number of the previous coin is small, put this number behind the number smaller than him .
#include <stdio.h>
void InsertSort(int array[], int len)
{
int i;
int j;
int temp;
for(i = 1; i < len; i++)
{
if(array[i] < array[i-1])
{
temp = array[i];
for(j = i - 1; j >= 0 && array[j] > temp; j--)
{
array[j+1] = array[j];
}
array[j+1] = temp;
}
}
}
void print(int array[], int len)
{
for(int i = 0; i < len; i++)
{
printf("%d ",array[i]);
}
}
int main()
{
int array[10] = {23,1,3,54,32,4,56,0,34,8};
print(array, 10);
printf("\n");
InsertSort(array, 10);
print(array, 10);
return 0;
}
2. Selection sort
Selection sorting is to start with the first number of a group of data , In the array, the subscript is from 0 Start , Record the position of the first number , Compare with the following numbers one by one , If there is a number smaller than this number , Then replace the position of this number with the position of the previously recorded number , Keep comparing until the last number , Then put the smallest number behind the ordered data , One analogy . After ranking, there is no need to compare , Compare back from the position that has not been sorted .
#include <stdio.h>
void SelectionSort(int array[], int len)
{
int i,j,k,temp;
for(i = 0; i < len; i++)
{
k = i;
for(j = i + 1; j < len; j++)
{
if(array[j] < array[k])
{
k = j;
}
}
temp = array[k];
array[k] = array[i];
array[i] = temp;
}
}
void print(int array[], int len)
{
for(int i = 0; i < len; i++)
{
printf("%d ",array[i]);
}
}
int main()
{
int array[10] = {23,1,3,54,32,4,56,0,34,8};
print(array, 10);
printf("\n");
SelectionSort(array, 10);
print(array, 10);
return 0;
}
3. Bubble sort
#include <stdio.h>
void BubbleSort(int array[], int len)
{
int i,j,k,temp;
for(i = 0; i < len; i++)
{
for(j = 0; j < len - i - 1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
void print(int array[], int len)
{
for(int i = 0; i < len; i++)
{
printf("%d ",array[i]);
}
}
int main()
{
int array[10] = {
23,1,3,54,32,4,56,0,34,8};
print(array, 10);
printf("\n");
BubbleSort(array, 10);
print(array, 10);
return 0;
}
边栏推荐
- 企业如何跨越数字化鸿沟?尽在云原生2.0
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
- Feature scaling normalization
- Practice and principle of PostgreSQL join
- Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
- [Lua] Int64 support
- 10 schemes to ensure interface data security
- Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
- Logo special training camp Section IV importance of font design
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
猜你喜欢
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
串口数据帧
Three stage operations in the attack and defense drill of the blue team
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Logo special training camp section II collocation relationship between words and graphics
Redis的持久化机制
Redis入门完整教程:HyperLogLog
随机推荐
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Attack and defense world misc advanced area ditf
Redis入门完整教程:Bitmaps
Logo special training camp section 1 Identification logo and logo design ideas
PostgreSQL server programming aggregation and grouping
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Deployment of JVM sandbox repeater
攻防世界 MISC 进阶区 3-11
Shell script implements application service log warehousing MySQL
攻防世界 MISC 进阶 glance-50
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
SPSS安装激活教程(包含网盘链接)
浅聊一下中间件
PMO: compare the sample efficiency of 25 molecular optimization methods
攻防世界 misc 进阶区 2017_Dating_in_Singapore
MYSQL架构——用户权限与管理
Redis入门完整教程:HyperLogLog
攻防世界 MISC 进阶区 Ditf
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task