【问题标题】:C Merge sort stuck in an infinite loopC合并排序陷入无限循环
【发布时间】:2016-07-15 01:45:34
【问题描述】:

我的程序使用递归进行分而治之,但由于未知原因,我得到了一个无限循环。我仍然得到未排序的数组作为答案

/**
 * MergeSort.c
 *
 * Uses recursion for divide and conquer
 * 
 *
 * Implements merge sort for an array
 */

初始化合并和归并排序的方法调用

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);

初始化显示函数

void display(int array[6]);

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}

void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while (beg<end)
      {
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }

 //merges two subarrays
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for storing the sorted elements
     int k=beg;     
     int i=beg;   //initialize the pointers for two sub arrays
     int j=mid;

     while (i<mid && j<end)
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                 k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                 k++;
              }              
        }

    //Clearing any remaining elements in the sub array
     while (i<mid) 
      {
           temp[k]=array[i];
           i++;
           k++; 
      }

    //Clearing any remaining elements in the sub array
    while (j<end) 
      {
          temp[k]=array[j];
          j++;
          k++; 
      }

    //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++)     
      {
          array[i]=temp[k];                                      
      }
    //prints the individual array elements  
    display(array);           //display array
}

//Displays the entire array

void display(int array[6])
{
     //prints the individual array elements
    for (int i=0;i<6;i++)
      {
             printf("%d ",array[i]); //prints the individual array elements
      }
     printf("\n"); //Enter a new line after every iteration
}

【问题讨论】:

    标签: recursion mergesort divide-and-conquer


    【解决方案1】:

    嗯,从mergesort函数,

    while (beg<end)
      {
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
    

    beg 和 end 不会被编辑。所以它将永远在这个循环中做一些事情。 我认为你应该从 while 改为 if

    此外,如果您的结尾是 5(该数组/段中的最后一个索引),则该段中的最后一个元素将不会被排序。这意味着这个数组不会被排序。您必须更改为...

     while (i<mid && j<=end) //from j<end to j<=end and for another loop
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                 k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                 k++;
              }              
        }
    

    祝你在 DQ 上好运

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多