Recursion C合并排序卡在无限循环中

Recursion C合并排序卡在无限循环中,recursion,mergesort,divide-and-conquer,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],

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

/**
 * 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);
#包括
#包括
无效合并(int数组[6],int beg,int mid,int end);
void mergesort(int数组[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
}
void显示(int数组[6]);
内部主(空)
{
//初始化数组
int数组[6]={3,1,2,9,5,4};
//初始化开始和结束
int beg=0,end=5;
//实现合并排序
合并排序(数组、beg、end);
getch();
}
void mergesort(int数组[6],int beg,int end)//调用初始合并排序
{
int mid;
mid=(beg+end)/2;

而(beg嗯,从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
  }

while(beg嗯,从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
  }
while(beg)