当前位置:网站首页>插入排序,选择排序,冒泡排序
插入排序,选择排序,冒泡排序
2022-07-04 19:56:00 【冬雪如春】
1.插入排序
大白话理解原理:将第一个数看成是有序的,从第二个数开始和该数前面的数进行比较,(默认从小到大排序)如果这个数比前一个数小,那么就将这个数,放在一个临时变量中,再进行相同的操作,知道发现前一个数币这个数小就将这个数放在比他小的那个数的后面。
#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.选择排序
选择排序就是从一组数据的第一个数开始,在数组中下标是从0开始,将第一个数的位置记录下来,在一个一个与后面的数进行比较,如果后面有比这个数还小的数,那么就将这个数的位置与之前记录的数的位置更换,一直这样比较到最后一个数,然后将这个最小的数放在排好序的数据的后面,一次类推。排完序的就可以不用比较了,从还未排序的位置开始往后比较。
#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.冒泡排序
#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;
}
边栏推荐
- [1200. Minimum absolute difference]
- E-week finance | Q1 the number of active people in the insurance industry was 86.8867 million, and the licenses of 19 Payment institutions were cancelled
- Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
- Après l'insertion de l'image dans le mot, il y a une ligne vide au - dessus de l'image, et la disposition est désordonnée après la suppression
- Android原生数据库的基本使用和升级
- Go language notes (4) go common management commands
- 网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??
- 伦敦银走势图分析的新方法
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- 黄金k线图中的三角形有几种?
猜你喜欢

Sword finger offer II 80-100 (continuous update)

【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”

What if the brightness of win11 is locked? Solution to win11 brightness locking

网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

【1200. 最小絕對差】

Foxit pdf editor v10.1.8 green version

实操自动生成接口自动化测试用例

黄金k线图中的三角形有几种?

Some suggestions for interface design
随机推荐
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
HWiNFO硬件检测工具v7.26绿色版
Go notes (3) usage of go language FMT package
hash 表的概念及应用
go语言笔记(2)go一些简单运用
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
RFID仓储管理系统解决方案的优点
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
word中插入图片后,图片上方有一空行,且删除后布局变乱
卷积神经网络在深度学习中新发展的5篇论文推荐
word中使用自动插入题注功能
软件开发过中的采购
Jmeter 之压测入门
[micro service SCG] use of predict
Play the music of youth
Android原生数据库的基本使用和升级
HMS Core 机器学习服务
Jiuqi ny8b062d MCU specification /datasheet