当前位置:网站首页>[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
边栏推荐
- 指針:最大值、最小值和平均值
- MySQL learning notes (stage 1)
- 数字电路基础(五)算术运算电路
- 【指针】求字符串的长度
- Intel oneapi - opening a new era of heterogeneity
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- c语言学习总结(上)(更新中)
- Interview Essentials: what is the mysterious framework asking?
- The most popular colloquial system explains the base of numbers
- JDBC transactions, batch processing, and connection pooling (super detailed)
猜你喜欢
Windows platform mongodb database installation
关于交换a和b的值的四种方法
Realize applet payment function with applet cloud development (including source code)
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
Applet Web Capture -fiddler
JDBC read this article is enough
Proceedingjoinpoint API use
Matplotlib绘图快速入门
数字电路基础(五)算术运算电路
SystemVerilog discusses loop loop structure and built-in loop variable I
随机推荐
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
函数:求方程的根
XSS unexpected event
Realize applet payment function with applet cloud development (including source code)
Ucos-iii learning records (11) - task management
Build domain environment (win)
指針:最大值、最小值和平均值
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
《英特尔 oneAPI—打开异构新纪元》
安全面试之XSS(跨站脚本攻击)
内网渗透之内网信息收集(二)
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
JVM memory model concept
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
指针--剔除字符串中的所有数字
JDBC read this article is enough
浅谈漏洞发现思路
链队实现(C语言)
Binary search tree concept
浙大版《C语言程序设计实验与习题指导(第3版)》题目集