简体   繁体   English

字符串排序与合并排序

[英]String Sort With Merge Sort

I am trying to do a string sort using merge sort algorithm. 我正在尝试使用合并排序算法进行字符串排序。 Here is my related code: 这是我的相关代码:

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]);
    }
}

The problem is tmp is returning with null values to sortCities function. 问题是tmp将null值返回给sortCities函数。 I checked the content of the tmp before I sent it to mergeSort and I saw that it copied right(from expedition.city). 在将tmp的内容发送到mergeSort之前,我检查了它的内容,然后看到它复制正确(来自expedition.city)。 What can be the reason? 可能是什么原因?

Edit: I handled that by writing "array[i] = newArray[j];" 编辑:我通过写“ array [i] = newArray [j];” insead "strcpy(array[i], newArray[j])". insead“ strcpy(array [i],newArray [j])”。 But now it does not sort correctly. 但是现在它不能正确排序。 I think I should use "<=" instead "<" but this time it stopped executing because of the limits of pointer arrays I guess. 我认为我应该使用“ <=”而不是“ <”,但是这次由于我猜想的指针数组的限制而停止执行。

I just used normal char matrix (char[][N]) instead pointer matrix and it worked. 我只是使用普通的char矩阵(char [] [N])代替了指针矩阵,所以它起作用了。 Here is my new code: 这是我的新代码:

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]);
}   

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM