0

我正在尝试外部合并包含已排序字符串的多个文件(基于 strcmp),但在使用外部合并排序时遇到问题。

似乎我的合并排序方法运行不正常,虽然数据被正确读入mergeSort(),并且算法只是一个典型的迭代合并,我想可能是因为 strcmp 在这种排序情况下不合适?

下面的代码是我的代码段,我已将外部合并更改为内部合并以对其进行测试,但它仍然无法正常工作。

/* Iterative C program for merge sort */
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<time.h>

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
void merge(char  arr[][300], int l, int m, int r);
// Utility function to find minimum of two integers
int min(int x, int y) { return (x<y)? x :y; }
/* Iterative mergesort function to sort arr[0...n-1] */
void mergeSort(char arr[][300], int n )
{
   int curr_size;  // For current size of subarrays to be merged
                   // curr_size varies from 1 to n/2
   int left_start; // For picking starting index of left subarray
                   // to be merged
   for (curr_size=1; curr_size<= n-1 ; curr_size = 2 * curr_size ){
       // Pick starting point of different subarrays of current size
       for ( left_start=0 ; left_start < n-1 ; left_start += 2 * curr_size ){
           // Find ending point of left subarray. mid+1 is starting
           // point of right
           int mid = left_start + curr_size - 1;
           int right_end = min( left_start + 2 * curr_size - 1 , n-1);
           if( mid > n-1 ){
               mid = (left_start+right_end)/2;
           }

           // Merge Subarrays arr[left_start...mid] & arr[mid+1...right_end]
           merge(arr, left_start, mid, right_end);
       }
   }
}

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
void merge( char arr[][300] , int l , int m , int r )
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
    /* create temp arrays */
    char L[n1][300] , R[n2][300];
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        strcpy( L[i] , arr[l + i] );
    for (j = 0; j < n2; j++)
        strcpy( R[j] , arr[m + 1+ j] );
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2){
        if ( strcmp( L[i] , R[i] ) <= 0 ){
            strcpy( arr[k] , L[i] );
            i++;
        }
        else{
            strcpy ( arr[k] , R[j] );
            j++;
        }
        k++;
    }
    /* Copy the remaining elements of L[], if there are any */
    while (i < n1){
        strcpy( arr[k] , L[i] );
        i++;
        k++;
    }
    /* Copy the remaining elements of R[], if there are any */
    while (j < n2){
        strcpy( arr[k] , R[j] );
        j++;
        k++;
    }
}
/* Function to print an array */
void printArray(char  A[][300] , int size )
{
    int i;
    for (i=0; i < size; i++)
        printf("%d:%s \n", i , A[i]);
}
/* Driver program to test above functions */
int main()
{
    srand(time(NULL));
    FILE **openarray,*output;
    int loop,quantity;
    int tempmumber,allcheck;
    char name[300];
    quantity=20;
    int random;
    char data[quantity][300],*temp,test[300];

    printf("initial ok\n");
    for( loop = 0 ; loop < quantity ; loop++)
    {
        random = rand()%900;
        sprintf ( name ,"%d" , random );
        strcpy( data[loop] , name );
        printf("%d:%s\n",loop,data[loop]);
    }
    mergeSort( data , quantity );

    printArray( data , quantity );
    return 0;
}

感谢您提供的任何帮助!

4

1 回答 1

1

在盯着你的代码太久之后很难发现小错误,当这种情况发生时,我建议使用调试器并逐行单步执行。

strcmp( L[i] , R[i] )应该是strcmp(L[i], R[j])

于 2018-12-09T07:34:41.280 回答