0
我創造了這個功能歸併排序給定陣列歸併給奇怪的答案
#include <iostream>
using namespace std;
#include "sorting.h"
void mergesort_normal(int *A, int p, int r)
{
if (p < r) {
int middle = p + (r - p)/2;
mergesort_normal(A, p, middle);
mergesort_normal(A, middle +1, r);
merge_normal(A, p, middle, r);
}
}
void merge_normal (int *A, int p, int mid, int r)
{
int *helper = new int[r+1];
int h = p;
int i = r;
int j = mid +1;
int k = 0;
while((h<=mid)&&(j<=r))
{
if(A[h]<=A[j])
{
helper[i]=A[h];
h++;
}
else
{
helper[i]=A[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=r;k++)
{
helper[i]=A[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
helper[i]=A[k];
i++;
}
}
for(k=p;k<=r;k++)
A[k]=helper[k];
}
int main()
{
int a[5] = {3,5,6,7,2};
mergesort_normal(a,0,4);
for(int i=0;i<=4;i++)
cout<<a[i]<<" "<<endl;
system("PAUSE");
cout<<endl<<endl<<endl<<endl;
return 0;
}
我得到的輸出:
-842150451
-842150451
-842150451
-842150451
-842150451
上爲什麼發生這種情況的任何想法?另外,我將如何去把它變成一個Bitonic mergesort?任何幫助將不勝感激!
某些東西沒有被初始化。另外,不要......'我<= 4; ...'做'...;我<5; ...' – BWG
任何想法可能是什麼?我已經經歷了一百次,似乎無法找到它 – user2796815