注册 登录
编程论坛 C++教室

字符串的排序,哪出错了啊?麻烦各位了~

境善 发布于 2014-02-22 21:22, 466 次点击
#include<stdio.h>
#include<string.h>
#define MAXSIZE 100

void selectSort(char **str,int n)
{
int i,j,k;
char temp[MAXSIZE];

for(i=1;i<=n;i++)
    {
    k=i;
    for(j=i+1;i<=n;i++)
        if(strcmp(str[i],str[j]))
        {
        strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
        strcpy(str[j],temp);
        }
    }
}

void main()
{
    char *string[]={"i","hello","world","mini"};   
    selectSort(string,sizeof(string));
    for(int m=0;m<sizeof(string);m++)
              printf("%s\n",string[m]);
   

}
2 回复
#2
xufan2014-02-22 22:30
修改了下
程序代码:
#include<stdio.h>
#include<string.h>

#define MAXSIZE 100

void selectSort(char *str[],int n)
{
    int i,j;

    for(i=0;i<n-1;i++)
    {
        for(j=i;j<n - i - 1;j++)
        {
            if(strcmp(str[j],str[j+1]) > 0)
            {
                char *p = str[j];
                str[j] = str[j+1];
                str[j+1] = p;
            }
        }
    }
}

void main()
{
    char *string[]={"i","hello","world","mini"};  
    selectSort(string,sizeof(string)/sizeof(char*));
    for(int m=0;m<sizeof(string)/sizeof(char*);m++)
        printf("%s ",string[m]);
    printf("\n");


}

#3
境善2014-02-22 22:40
回复 2楼 xufan
收到~谢谢
1