当前位置:网站首页>【指针】使用插入排序法将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
边栏推荐
- The most popular colloquial system explains the base of numbers
- Network technology related topics
- List and data frame of R language experiment III
- Which is more advantageous in short-term or long-term spot gold investment?
- The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
- Hackmyvm target series (3) -visions
- 循环队列(C语言)
- 记一次api接口SQL注入实战
- HackMyvm靶機系列(3)-visions
- A complete collection of papers on text recognition
猜你喜欢

Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class

Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
![New version of postman flows [introductory teaching chapter 01 send request]](/img/0f/a41a39093a1170cc3f62075fd76182.jpg)
New version of postman flows [introductory teaching chapter 01 send request]

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

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

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

Xray and burp linkage mining

记一次edu,SQL注入实战

循环队列(C语言)

Library management system
随机推荐
DVWA (5th week)
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
XSS (cross site scripting attack) for security interview
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
【指针】求二维数组中最大元素的值
Record an API interface SQL injection practice
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
我的第一篇博客
《统计学》第八版贾俊平第四章总结及课后习题答案
Detailed explanation of network foundation
Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
网络基础详解
内网渗透之内网信息收集(三)
. Net6: develop modern 3D industrial software based on WPF (2)
What language should I learn from zero foundation. Suggestions
Proceedingjoinpoint API use
captcha-killer验证码识别插件
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Hackmyvm target series (1) -webmaster
Hcip -- MPLS experiment