20
2007

使用指针实现冒泡排序

#include <stdio.h>
#define N 5
void main()
{
int i,j,*ptr,a[N],t;
ptr=a;
printf(“输入大小为%d的数组:\n”,N);
for(i=0;i<N;i++)
scanf(“%d”,&a[i]);
for(i=0;i<N;i++)
{
for(j=0;j<N-i-1;j++)
{
if(a[j]<a[j+1])
{
t=*(ptr+j);
*(ptr+j)=*(ptr+j+1);
*(ptr+j+1)=t;
}
}
}
printf(“\n 排序后的数组是:\n”);
for(i=0;i<N;i++)
printf(“%d “,a[i]);
printf(“\n”);
}

相关日志

如转载请注明:转自Nffish's Blog [ http://nffish.com ]

本文链接地址:http://nffish.com/archives/47

Written by Nffish in: 未分类 | 标签:,

4 条评论 »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Powered By Wordpress Theme AEROTRONIC |
除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。