递归排序问题
有位网民说我的程序中mergeSort函数没有出口,所以死循环,请问一下怎样修改呢?
#include<stdio.h>void main(){ void mergeSort(int a[],int b[],int left,int right); int a[9]={5,9,4,3,7,12,35,2,13}; int b[9]; mergeSort(a,b,1,9); for(int i=0;i<9;i++) printf("%d ",b[i]);}void mergeSort(int a[],int b[],int left,int right){ void merge(int a[],int b[],int left,int i,int right); while(left<right) { int i=(left+right)/2; merge(a,b,left,i,right); mergeSort(a,b,left,i); mergeSort(a,b,i+1,right); }}void merge(int a[],int b[],int left,int i,int right){ int n=left; int m=i+1; for(int j=left;j<right+1;j++) { if(n<=i&&m<=right) { if(a[n]>a[m]) { b[j]=a[m]; m++; } else { b[j]=a[n]; n++; } } else { if(n>i) { b[j]=a[m]; m++; } else { b[j]=a[n]; n++; } } }}
#include<stdio.h>void main(){ void mergeSort(int a[],int b[],int left,int right); int a[9]={5,9,4,3,7,12,35,2,13}; int b[9]; mergeSort(a,b,0,8);//改 mergeSort(a,b,1,9); for(int i=0;i<9;i++) printf("%d ",b[i]);}void mergeSort(int a[],int b[],int left,int right){ void merge(int a[],int b[],int left,int i,int right); if(left<right) //改 while(left<right) { int i=(left+right)/2; //移到下面 merge(a,b,left,i,right); mergeSort(a,b,left,i); mergeSort(a,b,i+1,right); merge(a,b,left,i,right); }}void merge(int a[],int b[],int left,int i,int right){ int n=left; int m=i+1; for(int j=left;j<right+1;j++) { if(n<=i&&m<=right) { if(a[n]>a[m]) { b[j]=a[m]; m++; } else { b[j]=a[n]; n++; } } else { if(n>i) { b[j]=a[m]; m++; } else { b[j]=a[n]; n++; } } } for(int j=left;j<right+1;j++) a[j] = b[j]; //加}