当前位置:网站首页>[pointer] use the insertion sorting method to arrange n numbers from small to large
[pointer] use the insertion sorting method to arrange n numbers from small to large
2022-07-06 14:35:00 【|Light|】
requirement
Programming , Use insert sort to put n Sort the numbers from small to large .( Use a pointer to achieve )
Code
#include<stdio.h>
/* * This function is used to input a one-dimensional integer array , The input data is stored in the formal parameter pointer a(a Is the first address of a one-dimensional array ) in * Input data in 0 As an end sign ,0 It is not stored in the array nor included in the total number of input data * The return value is the number of input data */
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;
}
/* * This function implements the array (a Is the first address of a one-dimensional array , It is the array to be sorted ) Sort , After sorting, it is still stored in a in * n Is an array a The number of numbers to be sorted in */
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 function
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;
}
test
Test input
1 2 7 6 8 5 4 2 0
Output
1 2 2 4 5 6 7 8
边栏推荐
猜你喜欢

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

Network technology related topics

Ucos-iii learning records (11) - task management

Internet Management (Information Collection)

servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。

Low income from doing we media? 90% of people make mistakes in these three points
![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事务、批处理以及连接池(超详细)

网络基础之路由详解

“人生若只如初见”——RISC-V
随机推荐
Wu Enda's latest interview! Data centric reasons
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
List and data frame of R language experiment III
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
数据库多表链接的查询方式
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Load balancing ribbon of microservices
《英特尔 oneAPI—打开异构新纪元》
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
SQL injection
1.支付系统
This article explains in detail how mockmvc is used in practical work
Numpy快速上手指南
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
记一次api接口SQL注入实战
JVM memory model concept
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案