当前位置:网站首页>插入排序,选择排序,冒泡排序
插入排序,选择排序,冒泡排序
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;
}
边栏推荐
- idea插件
- Hands on deep learning (III) -- convolutional neural network CNN
- See how Tencent does interface automation testing
- Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
- [1200. Minimum absolute difference]
- mysql语句执行详解
- hash 表的概念及应用
- [Shenbo introduction] VI How to contact your favorite doctoral tutor
- HWiNFO硬件检测工具v7.26绿色版
- Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
猜你喜欢
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
【1200. 最小絕對差】
PS vertical English and digital text how to change direction (vertical display)
How to solve the problem that win11 cannot write the value to the registry key?
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
Reinforcement learning - learning notes 2 | value learning
Leetcode+ 81 - 85 monotone stack topic
测试员的算法面试题-找众数
What if the brightness of win11 is locked? Solution to win11 brightness locking
随机推荐
JS卡牌样式倒计时天数
What if the computer page cannot be full screen? The solution of win11 page cannot be full screen
go defer的使用说明
Golang中UTF编码和字符集
Why is TCP three handshakes and four waves
扩展你的KUBECTL功能
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
How does the computer save web pages to the desktop for use
Function analysis and source code of hash guessing game system development
[micro service SCG] use of predict
See how Tencent does interface automation testing
RFID仓储管理系统解决方案的优点
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
Hash quiz game system development how to develop hash quiz game system development (multiple cases)
hash 表的概念及应用
Stack: how to realize the judgment of valid brackets?
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
go语言笔记(4)go常用管理命令
What if the win11 shared file cannot be opened? The solution of win11 shared file cannot be opened
colResizable.js自动调整表格宽度插件