我想对数组进行排序。所以我写了这个合并排序,它没有做我想要的,即排序,只是停顿!我一遍又一遍地检查算法,我觉得这很正确,但是没有!请看一下并告诉我可能出了什么问题。
void mergeSort(int *arr, int low, int high){
int mid = (low+high)/2;
while(low<high){
mergeSort(arr, low, mid);
mergeSort(arr, mid+1, high);
merge(arr, low, high, mid);
}
}
void merge(int *arr,int low, int high, int mid){
int i =low,j=mid+1,k=0;
int temp[50]; // should i new/malloc this with size of ( high -low +1) ?
while(i<=mid && j<=high){
if(arr[i]<arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while(i<=mid)
temp[k++] = arr[i++];
while(j<=high)
temp[k++] = arr[j++];
for(int x = 0; x<=high; x++){
arr[x]=temp[x];
}
}