x******a 发帖数: 6336 | 1 请问这个mergesort有什么问题?编译时没问题, 执行时提示Segmentation fault 11
多谢。
#include
using namespace std;
void merge(int a[], int const left, int const mid, int const right)
{
int* b=new int[right-left];
int h,i,j,k;
h=left;
i=0;
j=mid;
//merges the two array's into b[] till the first is finished
while((h
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
//completes the array filling in it the missing values
if(h==mid)
{
for(k=j;k
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h; k
{
b[i]=a[k];
i++;
}
}
//prints into the original array
for(k=0; k
{
a[k+left]=b[k];
}
delete[] b;
}
void MergeSort( int a[], int const left, int const right)
{
int mid;
if(left
{
mid=(left+right)/2;
MergeSort(a,left, mid);
MergeSort(a,mid, right);
merge(a, left, mid, right);
}
}
int main()
{
int a[]={1,2,3,5,3};
MergeSort(a, 0, 5);
for(int i=0; i<5; i++)
cout<
return 0;
} | t****t 发帖数: 6806 | 2 C/C++ use 0-based array, so a[right] is illegal.
11
【在 x******a 的大作中提到】 : 请问这个mergesort有什么问题?编译时没问题, 执行时提示Segmentation fault 11 : 多谢。 : #include : using namespace std; : void merge(int a[], int const left, int const mid, int const right) : { : int* b=new int[right-left]; : int h,i,j,k; : h=left; : i=0;
| x******a 发帖数: 6336 | 3 thrust: thank you for your reply.
I modified the code a little bit by getting ride of a[right] but I still get
the mistake. Could you please have a look if there is any other error?
Thank you.
【在 t****t 的大作中提到】 : C/C++ use 0-based array, so a[right] is illegal. : : 11
| x******a 发帖数: 6336 | 4 I found if I change the following code
if(left
I get the right sort.
void MergeSort( int a[], int const left, int const right)
{
int mid;
if(left
{
mid=(left+right)/2;
MergeSort(a,left, mid);
MergeSort(a,mid, right);
merge(a, left, mid, right);
}
}
get
【在 x******a 的大作中提到】 : thrust: thank you for your reply. : I modified the code a little bit by getting ride of a[right] but I still get : the mistake. Could you please have a look if there is any other error? : Thank you.
| p*****e 发帖数: 53 | 5 如果用left
下就知道了
【在 x******a 的大作中提到】 : I found if I change the following code : if(left: I get the right sort. : void MergeSort( int a[], int const left, int const right) : { : int mid; : if(left: { : mid=(left+right)/2; : MergeSort(a,left, mid);
| x******a 发帖数: 6336 | 6 Thank you Pinkiee,
I have another question,
when I use the following code to get an array. if I let N=2, then each time
I got 7 and 49.
Is there anything wrong, what should I do?
{
cout<< "enter a number"<
int N;
cin >>N;
int a[N];
for(int i=0; i
a[i]=rand()%100;
cout<
}
array跟一
【在 p*****e 的大作中提到】 : 如果用left: 下就知道了
| a9 发帖数: 21638 | 7 int rand ( void );
Generate random number
Returns a pseudo-random integral number in the range 0 to RAND_MAX.
This number is generated by an algorithm that returns a sequence of
apparently non-related numbers each time it is called. This algorithm uses a
seed to generate the series, which should be initialized to some
distinctive value using srand.
time
【在 x******a 的大作中提到】 : Thank you Pinkiee, : I have another question, : when I use the following code to get an array. if I let N=2, then each time : I got 7 and 49. : Is there anything wrong, what should I do? : { : cout<< "enter a number"<: int N; : cin >>N; : int a[N];
| p*****e 发帖数: 53 | 8 add those two lines in your program
#include // For time()
srand(time(0)); // Initialize random number generator.
time
【在 x******a 的大作中提到】 : Thank you Pinkiee, : I have another question, : when I use the following code to get an array. if I let N=2, then each time : I got 7 and 49. : Is there anything wrong, what should I do? : { : cout<< "enter a number"<: int N; : cin >>N; : int a[N];
| x******a 发帖数: 6336 | 9 Thank you a9 and again, pinkiee. It works great after I add the two lines:) |
|