您的位置:首页 > 职场人生

面试 C 算法 排序

2017-06-27 00:00 232 查看
摘要: 用C语言写出一个将字符串数组进行排序的程序
按降序排序 ,长度越长排最左边,长ॷ...

#include <stdio.h>
#include<string.h>
main()
{
char*p_str[8]={"Paris","York","London","Shanghai","Edo","Taipei","Beijing","Singapore"},*temp;
int i,j;
for(i=0;i<7;i++)//冒泡法排序
{
for(j=0;j<7-i;j++)
{
if(strlen(p_str[j])<strlen(p_str[j+1]))
{
temp=p_str[j];
p_str[j]=p_str[j+1];
p_str[j+1]=temp;
}
else
if(strlen(p_str[j])==strlen(p_str[j+1]))
if(strcmp(p_str[j],p_str[j+1])>1)
{temp=p_str[j];
p_str[j]=p_str[j+1];
p_str[j+1]=temp;}
}
}
for(i=0;i<8;i++)//输出字符串
printf("%s ",p_str[i]);
printf("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 字符串排序
相关文章推荐