当前位置:网站首页>[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
边栏推荐
- 函数:求方程的根
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- 《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
- The difference between layer 3 switch and router
- 函数:字符串反序存放
- Binary search tree concept
- Data mining - a discussion on sample imbalance in classification problems
- MSF generate payload Encyclopedia
- Lintcode logo queries the two nearest saplings
猜你喜欢
Intranet information collection of Intranet penetration (2)
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
JDBC read this article is enough
Intel oneapi - opening a new era of heterogeneity
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Xray and Burp linked Mining
记一次edu,SQL注入实战
Record an API interface SQL injection practice
Markdown font color editing teaching
浅谈漏洞发现思路
随机推荐
Network layer - simple ARP disconnection
安全面试之XSS(跨站脚本攻击)
Constants, variables, and operators of SystemVerilog usage
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Load balancing ribbon of microservices
攻防世界MISC练习区(gif 掀桌子 ext3 )
“Hello IC World”
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
On the idea of vulnerability discovery
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
List and data frame of R language experiment III
四元数---基本概念(转载)
【指针】数组逆序重新存放后并输出
Based on authorized access, cross host, and permission allocation under sqlserver
Uibutton status exploration and customization
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
Sword finger offer 23 - print binary tree from top to bottom