当前位置:网站首页>【指针】使用插入排序法将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
边栏推荐
- Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
- How to turn wechat applet into uniapp
- 内网渗透之内网信息收集(三)
- XSS之冷门事件
- Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
- 线程的实现方式总结
- JDBC transactions, batch processing, and connection pooling (super detailed)
- Intranet information collection of Intranet penetration (I)
- Harmonyos JS demo application development
- MySQL interview questions (4)
猜你喜欢

记一次,修改密码逻辑漏洞实战

JDBC transactions, batch processing, and connection pooling (super detailed)

Data mining - a discussion on sample imbalance in classification problems

Xray and Burp linked Mining

《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案

Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class

Build domain environment (win)

Low income from doing we media? 90% of people make mistakes in these three points

Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary

1.支付系统
随机推荐
Intranet information collection of Intranet penetration (3)
Load balancing ribbon of microservices
《统计学》第八版贾俊平第一章课后习题及答案总结
Web vulnerability - File Inclusion Vulnerability of file operation
内网渗透之内网信息收集(四)
The difference between layer 3 switch and router
Captcha killer verification code identification plug-in
JDBC看这篇就够了
我的第一篇博客
内网渗透之内网信息收集(三)
关于交换a和b的值的四种方法
Record once, modify password logic vulnerability actual combat
数据库多表链接的查询方式
New version of postman flows [introductory teaching chapter 01 send request]
{1,2,3,2,5}查重问题
网络基础详解
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Intranet information collection of Intranet penetration (2)
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
7-11 mechanic mustadio (PTA program design)