当前位置:网站首页>插入排序,选择排序,冒泡排序
插入排序,选择排序,冒泡排序
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;
}
边栏推荐
- What if win11u disk refuses access? An effective solution to win11u disk access denial
- word中使用自动插入题注功能
- acwing 3302. Expression evaluation
- [Shenbo introduction] VI How to contact your favorite doctoral tutor
- Jmeter 之压测入门
- PS vertical English and digital text how to change direction (vertical display)
- uniapp 富文本编辑器使用
- 分析伦敦银走势图的技巧
- js 闭包
- go语言笔记(4)go常用管理命令
猜你喜欢

render函数与虚拟dom

How does win11 search for wireless displays? Win11 method of finding wireless display device

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system

测试员的算法面试题-找众数

多模輸入事件分發機制詳解

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例

Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)

Explication détaillée du mécanisme de distribution des événements d'entrée multimodes

五子棋 上班摸鱼工具 可局域网/人机

What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
随机推荐
记录线上bug解决list(未完待续7/4)
五子棋 上班摸鱼工具 可局域网/人机
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
word中插入图片后,图片上方有一空行,且删除后布局变乱
Hands on deep learning (III) -- convolutional neural network CNN
阿里测试师用UI自动化测试实现元素定位
LeetCode 871. 最低加油次数
After inserting a picture into word, there is a blank line above the picture, and the layout changes after deletion
Android原生数据库的基本使用和升级
LeetCode+ 81 - 85 单调栈专题
nmap扫描
PS竖排英文和数字文字怎么改变方向(变竖直显示)
LeetCode 8. String conversion integer (ATOI)
Quelques suggestions pour la conception de l'interface
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
Function analysis and source code of hash guessing game system development
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
From automation to digital twins, what can Tupo do?
go defer的使用说明
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?