C语言二维数组,选择排序问题,求教!
有一字符数组n[][10]={“book”,”well”,“hello”,“yellow”,”cell”,“swap”,“atomic”,“thanks”,“love”,...
有一字符数组n[ ][10] ={“book”, ”well”, “hello”, “yellow”,”cell”,“swap”, “atomic”, “thanks”,“love”, “detail”},利用选择排序然后再进行从小到大排序。
void Sortstring1(char n[][10],int str)
{
for (int i = 0; i < str-1; i++)
{
for (int j = i+1; j < str; j++)
{
int r = strcmp(n[j], n[j+1]);
if (r > 0)
{
char temp[10] = {"\0"};
strcpy(temp, n[j]);
strcpy(n[j], n[j+1]);
strcpy(n[j+1], temp);
}
}
}
}
int main(int argc, char * argv[]) {
printf("\n\n选择排序");
char n[ ][10] ={"book", "well", "hello", "yellow","cell","swap", "atomic", "thanks","love", "detail"};
Sortstring1(n, 10);
for (int i = 0; i < 10; i++)
{
printf("%s ",n[i]);
}
return 0;
不知道哪里出错了,打印出来的字符串不能按照要求排序 还是乱序的。。。。求教 展开
void Sortstring1(char n[][10],int str)
{
for (int i = 0; i < str-1; i++)
{
for (int j = i+1; j < str; j++)
{
int r = strcmp(n[j], n[j+1]);
if (r > 0)
{
char temp[10] = {"\0"};
strcpy(temp, n[j]);
strcpy(n[j], n[j+1]);
strcpy(n[j+1], temp);
}
}
}
}
int main(int argc, char * argv[]) {
printf("\n\n选择排序");
char n[ ][10] ={"book", "well", "hello", "yellow","cell","swap", "atomic", "thanks","love", "detail"};
Sortstring1(n, 10);
for (int i = 0; i < 10; i++)
{
printf("%s ",n[i]);
}
return 0;
不知道哪里出错了,打印出来的字符串不能按照要求排序 还是乱序的。。。。求教 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |