将若字符串按字母顺序输出(由小到大)----指针数组---选择法
#include
#include
int main()
{
void sort(char *name[],int n);
void print(char *name[],int n);
char *name[]={"banana","apple","candle","mooncake"};
int n=4;
sort(name,n);
print(name,n);
}
void sort(char *name[],int n)
{
char *temp;
int i,j,k;
for(i=0;i k=i;
for(j=i+1;j if(strcmp(name[k],name[j])>0)k=j;
if(k!=i)
{temp=name[i];name[i]=name[k];name[k]=temp;}
}
}
void print(char *name[],int n)
{
int i;
for(i=0;i
int i;
for(i=0;i printf("%s\n",name[i]);
}
阅读(1915) | 评论(0) | 转发(0) |