当前位置:网站首页>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;
}
边栏推荐
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
- Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
- LOGO特訓營 第一節 鑒別Logo與Logo設計思路
- Redis的持久化机制
- More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
- Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
- The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
- Redis入门完整教程:事务与Lua
- 9 - 类
- Advanced area of attack and defense world misc 3-11
猜你喜欢

Locust performance test - environment construction and use

Unity vscode emmylua configuration error resolution

Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
![[roommate learned to use Bi report data processing in the time of King glory in one game]](/img/06/22dde3fcc0456bd230e1d0cde339ec.png)
[roommate learned to use Bi report data processing in the time of King glory in one game]

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

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

Li Kou 98: verify binary search tree

MYSQL架构——逻辑架构

Talk about Middleware

LOGO special training camp section I identification logo and Logo Design Ideas
随机推荐
UML diagram memory skills
Common methods in string class
10 schemes to ensure interface data security
剑指 Offer 67. 把字符串转换成整数
串口数据帧
Analysis of environmental encryption technology
How to send a reliable request before closing the page
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
环境加密技术解析
Domestic database chaos
How to reset the password of MySQL root account
啃下大骨头——排序(二)
The difference between Max and greatest in SQL
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
About stack area, heap area, global area, text constant area and program code area
Sword finger offer 67 Convert a string to an integer
Redis入门完整教程:Redis Shell
剑指 Offer 65. 不用加减乘除做加法
md5工具类
LOGO特训营 第二节 文字与图形的搭配关系