在C

时间:2018-04-14 11:36:57

标签: c string algorithm merge

这是一个程序,需要两个未分类的数组DTA& DTB。使用冒泡排序方法对它们进行排序,然后使用合并算法,将两个数组合并为一个更大的数组(big_array)。泡泡分类工作正常。但是我的合并算法会导致程序崩溃。该程序将贯穿冒泡排序方面,但当遇到合并部分时,它会崩溃。

任何建议都会很棒!

#include <stdio.h>
#include <string.h>
int main() {
    //initialising variables and the arrays containing each of the four groups.
    //
    char *DTA [12] = { "James ", "John ", "Robert ", "Michael", "William ", "David ", "Richard ", "Joseph", "Thomas", "Charles", "Chris ", "Henry " };
    char *DTB [14] = { "Brian ", "Edward", "Ronald ", "Tim ", "Jason ", "Jeff ", "Geoff ", "Ryan ", "Gary ", "Jacob ", "Nicholas", "Eric ", "Nicholas", "Larry " };
    char *big_array [42];
    int index = 0;
    char temp[100];
    int n = sizeof(DTA) / sizeof(DTA[0]);
    int i = 0;
    int j = 0;
    int k = 0;

    //This is a bubble sorting Algorithm. It sorts DTA & DTB in order and then prints the sorted arrays out.
    //
    for (int j = 0; j < n - 1; j++) {
        for (int i = j + 1; i < n; i++) {
            if (strcmp(DTA[j], DTA[i]) > 0) {
                char *temp = DTA[j];
                DTA[j] = DTA[i];
                DTA[i] = temp;
            }//end if
            if (strcmp(DTB[j], DTB[i]) > 0) {
                char *temp = DTB[j];
                DTB[j] = DTB[i];
                DTB[i] = temp;
            }//end if
        }//end for
    }//end for
    //This is a merging algorithm. It merges DTA & DTB into the array big_array.
    //
    while (index < 12 && j < 14) {
        if (DTA[index] <= DTB[j]) {
            big_array[k] = DTA[index];
           index = index + 1;
        }//end if
        else
        {
            big_array[k] = DTB[j];
            j = j + 1;
            k = k + 1;
        }//end else
    }//end while
    while (j < 14) {
        big_array[k] = DTB[index];
        index = index + 1;
        k = k + i;
    }//end while
    for (int index = 0; index < n; index++)
        printf("\n String %d is %s", index+1, big_array[i]);

    getchar();
    getchar();
    return 0;
}//end main()

2 个答案:

答案 0 :(得分:1)

如果DTA移动,您错过k的增量,在DTB[j]尾部复制和忘记DTB[index]复制时出错:

while ( index < 12 && j < 14 )
{
    if ( strcmp(DTA[index], DTB[j]) <=0 )
    {
        big_array[k] = DTA[index] ;
        index = index + 1 ;
        k = k + 1 ;
    }//end if
    else
    {
        big_array[k] = DTB[j] ;
        j = j + 1 ;
        k = k + 1 ;
    }//end else
}//end while
while ( j < 14 )
{
    big_array[k] = DTB[j] ;
    j = j + 1 ;
    k = k + 1 ;
}//end while
while ( index < 12 )
{
    big_array[k] = DTA[index] ;
    index = index + 1 ;
    k = k + 1 ;
}//end while
for (int i=0; i<26; i++)
  printf("\n String %d is %s", i+1, big_array[i]);

我也纠正了输出for-loop(好像你没看过你写的东西)

还要考虑使用常量/变量而不是魔术值

答案 1 :(得分:-1)

以下提议的代码:

  1. 使用“冒泡排序”算法
  2. 对两个初始数组进行适当排序
  3. 正确合并两个已排序的数组
  4. 输出已排序的DTA []
  5. 的内容
  6. 输出已排序的DTB []
  7. 的内容
  8. 输出合并的big_array []
  9. 的内容

    注意:原始排序算法无法正常工作

    注意:原始合并算法无法正常工作

    现在,建议的代码:

    #include <stdio.h>
    #include <string.h>
    
    //initialising variables and the arrays containing each of the four groups.
    //
    char *DTA [12] =
    {
        "James Smith DT01 DT265A",
        "John Murphy DT02 DT265A",
        "Robert Lam DT03 DT265A",
        "Michael Martin DT04 DT265A",
        "William Brown DT05 DT265A",
        "David Roy DT06 DT265A",
        "Richard Tremblay DT07 DT265A",
        "Joseph Lee DT08 DT265A",
        "Thomas Gagnon DT09 DT265A",
        "Charles Wilson DT10 DT265A",
        "Chris Philips DT11 DT265A",
        "Henry Hill DT12 DT265A"
    } ;
    
    char *DTB [14] =
    {
        "Brian Smith DT23 DT265B",
        "Edward Brown DT24 DT265B",
        "Ronald Wilson DT25 DT265B",
        "Tim Robertson DT26 DT265B",
        "Jason Thomson DT27 DT265B",
        "Jeff Campbell DT28 DT265B",
        "Geoff Stewart DT29 DT265B",
        "Ryan Anderson DT30 DT265B",
        "Gary Scott DT31 DT265B",
        "Jacob Murray DT32 DT265B",
        "Nicholas MacDonald DT33 DT265B",
        "Eric Reid DT34 DT265B",
        "Nicholas Taylor DT35 DT265B",
        "Larry Clark DT36 DT265B"
    } ;
    
    
    int main( void )
    {
        size_t lengthDTA = sizeof(DTA)/sizeof(DTA[0]);
        size_t lengthDTB = sizeof(DTB)/sizeof(DTB[0]);
    
        char *big_array [ lengthDTA + lengthDTB ];
    
        //This is a bubble sorting Algorithm.
        //It sorts DTA & DTB in order and then
        //prints the sorted arrays out.
        //
        for ( size_t j = 0; j < lengthDTA - 1 ; j++ )
        {
            for ( size_t i = j + 1 ; i < lengthDTA ; i++ )
            {
                if( strcmp(DTA[j], DTA[i]) > 0 )
                {
                    char *temp = DTA[j] ;
                    DTA[j] = DTA[i] ;
                    DTA[i] = temp ;
                }//end if
            }//end for
        }//end for
    
        for ( size_t j = 0; j < lengthDTB - 1 ; j++ )
        {
            for ( size_t i = j + 1 ; i < lengthDTB ; i++ )
            {
              if( strcmp(DTB[j], DTB[i]) > 0 )
                {
                    char *temp = DTB[j] ;
                    DTB[j] = DTB[i] ;
                    DTB[i] = temp ;
                }//end if
            }//end for
        }//end for
    
    
        puts( "\nsorted DTA" );
        for( size_t k=0; k < lengthDTA; k++ )
        {
            printf( "DTA #%3.3lu = %s\n", k, DTA[k] );
        }
    
        puts( "\nsorted DTB" );
        for( size_t k=0; k < lengthDTB; k++ )
        {
            printf( "DTB #%3.3lu = %s\n", k, DTB[k] );
        }
    
        //This is a merging algorithm.
        //It merges DTA & DTB into the array big_array.
        //
        size_t i = 0;
        size_t j = 0;
        size_t index = 0;
    
        while( i < lengthDTA
            && j < lengthDTB )
        {
            if( strcmp( DTA[i], DTB[j] ) > 0 )
            {
                big_array[ index ] = DTA[i] ;
                i++;
            }
    
            else
            {
                big_array[ index ] = DTB[j] ;
                j++;
            }
            index++;
        }//end while
    
    
    
        while( i < lengthDTA )
        {
            big_array[ index ] = DTA[i];
            i++;
            index++;
        }
    
        while( j < lengthDTB )
        {
            big_array[ index ] = DTB[ j ];
            j++;
            index++;
        }
    
    
        puts("");
        for ( size_t index=0; index < (lengthDTA+lengthDTB); index++)
        {
            printf("Merged %3.3lu is %s\n", index+1, big_array[index]);
        }
    
        getchar();
        getchar();
        return 0;
    }//end main()
    

    结果输出:

    sorted DTA
    DTA #000 = Charles Wilson DT10 DT265A
    DTA #001 = Chris Philips DT11 DT265A
    DTA #002 = David Roy DT06 DT265A
    DTA #003 = Henry Hill DT12 DT265A
    DTA #004 = James Smith DT01 DT265A
    DTA #005 = John Murphy DT02 DT265A
    DTA #006 = Joseph Lee DT08 DT265A
    DTA #007 = Michael Martin DT04 DT265A
    DTA #008 = Richard Tremblay DT07 DT265A
    DTA #009 = Robert Lam DT03 DT265A
    DTA #010 = Thomas Gagnon DT09 DT265A
    DTA #011 = William Brown DT05 DT265A
    
    sorted DTB
    DTB #000 = Brian Smith DT23 DT265B
    DTB #001 = Edward Brown DT24 DT265B
    DTB #002 = Eric Reid DT34 DT265B
    DTB #003 = Gary Scott DT31 DT265B
    DTB #004 = Geoff Stewart DT29 DT265B
    DTB #005 = Jacob Murray DT32 DT265B
    DTB #006 = Jason Thomson DT27 DT265B
    DTB #007 = Jeff Campbell DT28 DT265B
    DTB #008 = Larry Clark DT36 DT265B
    DTB #009 = Nicholas MacDonald DT33 DT265B
    DTB #010 = Nicholas Taylor DT35 DT265B
    DTB #011 = Ronald Wilson DT25 DT265B
    DTB #012 = Ryan Anderson DT30 DT265B
    DTB #013 = Tim Robertson DT26 DT265B
    
    Merged 001 is Charles Wilson DT10 DT265A
    Merged 002 is Chris Philips DT11 DT265A
    Merged 003 is David Roy DT06 DT265A
    Merged 004 is Henry Hill DT12 DT265A
    Merged 005 is James Smith DT01 DT265A
    Merged 006 is John Murphy DT02 DT265A
    Merged 007 is Joseph Lee DT08 DT265A
    Merged 008 is Michael Martin DT04 DT265A
    Merged 009 is Richard Tremblay DT07 DT265A
    Merged 010 is Robert Lam DT03 DT265A
    Merged 011 is Thomas Gagnon DT09 DT265A
    Merged 012 is William Brown DT05 DT265A
    Merged 013 is Brian Smith DT23 DT265B
    Merged 014 is Edward Brown DT24 DT265B
    Merged 015 is Eric Reid DT34 DT265B
    Merged 016 is Gary Scott DT31 DT265B
    Merged 017 is Geoff Stewart DT29 DT265B
    Merged 018 is Jacob Murray DT32 DT265B
    Merged 019 is Jason Thomson DT27 DT265B
    Merged 020 is Jeff Campbell DT28 DT265B
    Merged 021 is Larry Clark DT36 DT265B
    Merged 022 is Nicholas MacDonald DT33 DT265B
    Merged 023 is Nicholas Taylor DT35 DT265B
    Merged 024 is Ronald Wilson DT25 DT265B
    Merged 025 is Ryan Anderson DT30 DT265B
    Merged 026 is Tim Robertson DT26 DT265B