C合并排序陷入无限循环

时间:2016-03-27 19:14:49

标签: recursion mergesort divide-and-conquer

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

/**
 * 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
}

1 个答案:

答案 0 :(得分:0)

好吧,来自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
  }

求得和结束不会被编辑。所以它会永远在这个循环中做点什么。 我认为你应该从改为如果

此外,如果你的结尾是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&lt; 3