简体   繁体   English

合并字符串C中的字符

[英]Merge sort of characters in string C

I have this working merge-sort algorithm in C. But it works only for integers. 我在C中有这个工作的合并排序算法。但是它仅适用于整数。 When I tried to change int to char, i'm getting segfault. 当我尝试将int更改为char时,我遇到了段错误。

Can you please help me, what should I change in this code, so I could use MergeSort like this: 您能帮我吗,我应该在代码中进行哪些更改,以便可以像这样使用MergeSort:

char*str = "test_string";
MergeSort(str, 0, strlen(str)-1);

void Merge(int *array, int left, int mid, int right){

    int tempArray[right-left+1];
    int pos=0,lpos = left,rpos = mid + 1;

    while(lpos <= mid && rpos <= right){
            if(array[lpos] <= array[rpos]){
                    tempArray[pos++] = array[lpos++];
            }
            else{
                    tempArray[pos++] = array[rpos++];
            }
    }

    while(lpos <= mid)  tempArray[pos++] = array[lpos++];
    while(rpos <= right)tempArray[pos++] = array[rpos++];

    int iter;
    for(iter = 0;iter < pos; iter++){
            array[iter+left] = tempArray[iter];
    }

    return;
}

void MergeSort(int *array, int left, int right){
    int mid = (left+right)/2;

    if(left<right){
            MergeSort(array,left,mid);
            MergeSort(array,mid+1,right);
            Merge(array,left,mid,right);
    }
    return;
}

I'm lost. 我迷路了。 Thanks! 谢谢!

Change your declaration of array from int * to char * in both functions. 在两个函数中,将array的声明从int *更改为char * Make tempArray a char[] instead of an int[] . tempArray设为char[]而不是int[] You are trying to read memory that is 4x (or 8x) out of bounds at the end of the array, hence the seg-fault. 您正在尝试读取超出数组末尾4倍(或8倍)的内存,因此出现了段错误。 Put another way, char is 1 byte (usually) while int is 4 or 8, so you are looking at items of a different size stacked next to each other. 换句话说, char是1个字节(通常),而int是4或8,因此您正在查看彼此相邻堆叠的不同大小的项目。 Also, do not pass in a const * for your string. 另外,请勿为字符串传递const * Declaring a string as char*str = "test_string"; 将字符串声明为char*str = "test_string"; implies read-only memory on some systems. 表示某些系统上的只读存储器。 Use char str[] = "test_string"; 使用char str[] = "test_string"; instead. 代替。 If you are not using strictly C, you can use C++ templates to make a function that works for int and char : http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1 如果您不严格使用C,则可以使用C ++模板来创建适用于intchar的函数: http : //www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-第1部分

#include <stdio.h>

#include<ctype.h>

#include<string.h>

int Run_count=-1;

int main ( int argc , char *argv[] )
{ 

    /* if you dont want to use argv, put the elements in A yourself,
    size being the number of string*/

    /*L --> left side, R --> right side*/

    int i = 0;

    int size = argc-1;

    char *A[argc-1];

    for(i=1;i<=argc;i++){*(A+i-1) = argv[i];}

    Caller(A,size);

    for(i=0;i<size;i++){

        printf("%s\n", A[i]);

    }

    printf("%d",Run_count);
}

int Caller(char* A[] , int n){

    Run_count++;

    int sizeL, sizeR ,i;

    char *L[n/2+1] , *R[n-n/2+1];

    if (n < 2){return 1;}

    sizeL = n/2;
    sizeR = n - sizeL;

    for(i=0;i<sizeL;i++)  {L[i] = *(A+i);}
    for(i=0;i<n - n/2;i++)  {R[i] = *(A+i+n/2);}

    Caller( L, sizeL);
    Caller( R, sizeR);
    merger( L,sizeL, R,sizeR, A);
}

void merger(char* L[], int lengthL , char* R[] , int lengthR , char *A[]){

    int i, j, k ,t =0 ;

    for(k = 0 , j = 0; k < lengthL && j < lengthR ;t++){

        if(compare(*(L+k),*(R+j))){
            *(A+t) = *(L+k);
            k++;}

        else{*(A+t) = *(R+j);j++;}
    }

    while(k < lengthL ){
        *(A+t) = *(L+k); 
        k++;t++;
        }

    while(j < lengthR ){
        *(A+t) = *(R+j);
        j++;t++;}
}

int compare(char *line1 , char *line2 )
{
    int i;

    for(i = 0;*(line1 + i) != '\0' && *(line2 + i) != '\0' ;){
        if(isdigit(*(line1+i)) && isalpha(*(line2+i))){return 0;}

        else if(isdigit(*(line2+i)) && isalpha(*(line1+i))){return 1;}

        else if(*(line1 + i) > *(line2 + i)){return 0;}

        else if(*(line1 + i) == *(line2 + i)){i++;}

        else{return 1;}
    }
}

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

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