【问题标题】:Merging two sorted string arrays in C在C中合并两个排序的字符串数组
【发布时间】:2018-09-24 15:22:18
【问题描述】:

这是一个采用两个未排序数组 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()

【问题讨论】:

  • 你得到什么错误信息或堆栈跟踪?

标签: c string algorithm merge


【解决方案1】:

以下建议的代码:

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

注意:原来的排序算法不能正常工作

注意:原来的合并算法不能正常工作

现在,建议的代码:

#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

【讨论】:

    【解决方案2】:

    您在 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(似乎您没有阅读您所写的内容)

    还可以考虑使用常量/变量而不是魔法值

    【讨论】:

    • 非常感谢您的帮助!你为什么使用这行代码?这个功能是如何运作的? if ( strcmp(DTA[index], DTB[j])
    • 你在冒泡排序中使用了这一行 ;-) 如果没有strcmp,你比较的是地址,而不是字符串内容
    猜你喜欢
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多