当前位置:网站首页>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;
}
边栏推荐
- Serial port data frame
- Practice and principle of PostgreSQL join
- 华泰证券是国家认可的券商吗?开户安不安全?
- 攻防世界 MISC 进阶区 can_has_stdio?
- Redis入门完整教程:慢查询分析
- 攻防世界 misc 高手进阶区 a_good_idea
- On-off and on-off of quality system construction
- How to manage 15million employees easily?
- PMO: compare the sample efficiency of 25 molecular optimization methods
- How to send a reliable request before closing the page
猜你喜欢

Redis入门完整教程:客户端通信协议

Attack and defense world misc master advanced zone 001 normal_ png

Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code

Redis入门完整教程:Redis Shell

Business is too busy. Is there really no reason to have time for automation?

【机器学习】手写数字识别

Li Kou 98: verify binary search tree

Logo special training camp section II collocation relationship between words and graphics

Hit the core in the advanced area of misc in the attack and defense world

UML diagram memory skills
随机推荐
Redis入门完整教程:Bitmaps
Redis入门完整教程:GEO
攻防世界 MISC 进阶区 can_has_stdio?
LOGO特訓營 第三節 首字母創意手法
串口数据帧
How to reset the password of MySQL root account
测试必会:BUG的分类及推进解决
Why is Dameng data called the "first share" of domestic databases?
Breakpoint debugging under vs2019 c release
Practice and principle of PostgreSQL join
Microservices -- Opening
Duplicate ADMAS part name
[machine learning] handwritten digit recognition
啃下大骨头——排序(二)
攻防世界 MISC 进阶区 3-11
Attack and defense world misc advanced area can_ has_ stdio?
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
MySQL Architecture - user rights and management
Introducing QA into the software development lifecycle is the best practice that engineers should follow
The difference between Max and greatest in SQL