字符串使用合并排序排序

时间:2017-10-13 06:03:11

标签: c string merge mergesort

我正在尝试使用合并排序算法进行字符串排序。这是我的相关代码:

void sortCities(EXPEDITION *expedition, int expeditionNumber)
{
    int i;
    int j;
    char **tmp;
    tmp = (char**)malloc((expeditionNumber) * sizeof(char*));
    for (i = 0; i < expeditionNumber; i++) {
        tmp[i] = (char*)malloc(15 * sizeof(char));
    }
    for (i = 0, j = 0; i < expeditionNumber; i++, j++) {
        tmp[j] = expedition[i].city;
    }
    for (i = 0; i < expeditionNumber; i++) {
        printf("%s\n", tmp[i]);
    }
    mergeSort(tmp, 0, expeditionNumber);
    for (i = 0; i < expeditionNumber; i++) {
        printf("%s\n", tmp[i]);
    }
}

void mergeSort(char** array, int first, int last)
{
    int middle;
    if (first < last) {
        middle = (first + last) / 2;
        mergeSort(array, first, middle);
        mergeSort(array, middle + 1, last);
        merge(array, first, middle + 1, middle, last);
    }
}

void merge(char** array, int first1, int first2, int last1, int last2)
{
    int i;
    int j;
    int k;
    char **newArray;
    newArray = (char**)malloc((last2 - first1 + 1) * sizeof(char*));
    for (i = 0; i < last2 - first1 + 1; i++) {
        newArray[i] = (char*)malloc(15 * sizeof(char));
    }
    j = first2;
    i = first1;
    k = 0;
    while (i < last1 && j < last2) {
        if (strcmp(array[i], array[j]) > 0) {
            strcpy(newArray[k++], array[j++]);
        }
        else {
            strcpy(newArray[k++], array[i++]);
        }
    }
    if (i < last1) {
        while (i < last1) {
            strcpy(newArray[k++], array[i++]);
        }
    }
    if (j < last2) {
        while (j < last2) {
            strcpy(newArray[k++], array[j++]);
        }
    }
    for (i = first1, j = 0; i < last2; i++, j++) {
        strcpy(array[i], newArray[j]);
    }
}

问题是tmp返回null值到sortCities函数。在我将它发送到mergeSort之前检查了tmp的内容,我看到它被复制了(来自expedition.city)。可能是什么原因?

编辑:我通过编写&#34; array [i] = newArray [j];&#34;来处理它。 insead&#34; strcpy(array [i],newArray [j])&#34;。但现在它没有正确排序。我想我应该使用&#34;&lt; =&#34;相反&#34;&lt;&#34;但是这次它因为我猜的指针数组的限制而停止执行。

1 个答案:

答案 0 :(得分:0)

我只使用普通的char矩阵(char [] [N])而不是指针矩阵,它起作用了。这是我的新代码:

void merge(char array[100][15],int first1,int first2,int last1,int last2){
int i;
int j;
int k;
char **newArray;
newArray=(char **)malloc((last2-first1+1)*sizeof(char *));
for(i=0;i<last2-first1+1;i++){
    newArray[i]=(char *)malloc(15*sizeof(char));
}
j=first2; 
i=first1; 
k=0;
while(i<=last1 && j<=last2){

    if(strcmp(array[i],array[j])>0){ 
        strcpy(newArray[k++], array[j++]);
    }
    else{
        strcpy(newArray[k++], array[i++]);
    }
}

while(i<=last1){            
    strcpy(newArray[k++], array[i++]);
}

while(j<=last2){
    strcpy(newArray[k++], array[j++]);
}

for(i=first1,j=0;j<k;i++,j++){
    strcpy(array[i], newArray[j]);
}

}

void mergeSort(char array[100][15],int first,int last){ 
int middle;
if(first<last){
    middle=(first+last)/2;
    mergeSort(array,first,middle);
    mergeSort(array,middle+1,last);
    merge(array,first,middle+1,middle,last); 
}

}

void sortCities(EXPEDITION *expedition, int expeditionNumber){
int i;
int j;
char tmp[100][15];

for(i=0,j=0;i<expeditionNumber;i++,j++){
    strcpy(tmp[j],expedition[i].city);
}

mergeSort(tmp,0,expeditionNumber);
for(i=0;i<expeditionNumber;i++){
    printf("%s\n",tmp[i]);
}   

}