当前位置:网站首页>【指针】使用插入排序法将n个数从小到大进行排列
【指针】使用插入排序法将n个数从小到大进行排列
2022-07-06 09:24:00 【|光|】
要求
编写程序,使用插入排序法将n个数从小到大进行排序。(用指针实现)
代码
#include<stdio.h>
/* * 该函数用来输入一维整数数组,输入的数据存放在形参指针a(a为一维数组的首地址)中 * 输入的数据以0作为结束标志,0不存入数组也不计入输入数据的总数 * 返回值为输入数据的个数 */
int input(int *a)
{
int n=0;
int b = 0;
do
{
scanf("%d",&b);
if(b == 0)
break;
else
{
a[n] = b;
n++;
}
}
while(b != 0);
return n;
}
/* * 该函数实现对数组(a为一维数组的首地址,其为待排序的数组)进行排序,排序后仍然存放在a中 * n为数组a中待排序数的个数 */
void insert_sort(int *a,int n)
{
int i,j,key;
for (i=1;i<n;i++)
{
key = a[i];
j=i-1;
while((j>=0) && (a[j]>key))
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
}
main函数
int main()
{
int a[200],n;
n=input(a);
insert_sort(a,n);
for(int i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
测试
测试输入
1 2 7 6 8 5 4 2 0
输出
1 2 2 4 5 6 7 8
边栏推荐
- Detailed explanation of three ways of HTTP caching
- DVWA (5th week)
- Interpretation of iterator related "itertools" module usage
- "Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
- Hackmyvm target series (6) -videoclub
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- 《英特尔 oneAPI—打开异构新纪元》
- XSS unexpected event
- Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
猜你喜欢

《统计学》第八版贾俊平第七章知识点总结及课后习题答案

记一次edu,SQL注入实战

Intranet information collection of Intranet penetration (2)

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案

HackMyvm靶機系列(3)-visions

关于超星脚本出现乱码问题

Proceedingjoinpoint API use

Detailed explanation of network foundation

7-7 7003 combination lock (PTA program design)

SystemVerilog discusses loop loop structure and built-in loop variable I
随机推荐
Hackmyvm target series (2) -warrior
7-8 7104 Joseph problem (PTA program design)
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
What language should I learn from zero foundation. Suggestions
“人生若只如初见”——RISC-V
Which is more advantageous in short-term or long-term spot gold investment?
《统计学》第八版贾俊平第四章总结及课后习题答案
Markdown font color editing teaching
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
captcha-killer验证码识别插件
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Low income from doing we media? 90% of people make mistakes in these three points
Interpretation of iterator related "itertools" module usage
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Uibutton status exploration and customization
内网渗透之内网信息收集(一)
JDBC transactions, batch processing, and connection pooling (super detailed)
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
SQL注入