当前位置:网站首页>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;
}
边栏推荐
- Redis入门完整教程:客户端通信协议
- 攻防世界 misc 进阶区 2017_Dating_in_Singapore
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
- 繁华落尽、物是人非:个人站长该何去何从
- More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
- Logo special training camp Section IV importance of font design
- PMO: compare the sample efficiency of 25 molecular optimization methods
- Shell script implements application service log warehousing MySQL
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
猜你喜欢
Persistence mechanism of redis
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
SPSS installation and activation tutorial (including network disk link)
浅聊一下中间件
How to send a reliable request before closing the page
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Sobel filter
页面关闭前,如何发送一个可靠请求
攻防世界 MISC 进阶 glance-50
LOGO special training camp section I identification logo and Logo Design Ideas
随机推荐
Redis入门完整教程:HyperLogLog
华泰证券是国家认可的券商吗?开户安不安全?
攻防世界 MISC 进阶区 hit-the-core
MYSQL架构——用户权限与管理
Logo Camp d'entraînement section 3 techniques créatives initiales
Advanced area of attack and defense world misc 3-11
LOGO特訓營 第三節 首字母創意手法
Feature scaling normalization
Sword finger offer 68 - I. nearest common ancestor of binary search tree
特征缩放 标准化 归一化
Lost in the lock world of MySQL
啃下大骨头——排序(二)
攻防世界 misc 进阶区 2017_Dating_in_Singapore
攻防世界 MISC 进阶 glance-50
High school physics: linear motion
[Lua] Int64 support
攻防世界 MISC 高手进阶区 001 normal_png
Attack and defense world misc advanced area ditf
醒悟的日子,我是怎么一步一步走向软件测试的道路
LOGO特训营 第三节 首字母创意手法