Problem
I have a source code in Java for sorting array elements using the merge sort
algorithm. The principle uses a loop to compare an element in the array with the element in the next index in the array. If the earlier is bigger than the later then the numbers are swapped logically to reassign
the array elements in the indices.
My problem is that the Java algo works but the C++ algo does not. The logic is the same, what am I doing wrong....
Code
Working Java Code
static void sort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
C++ Code built on the same merge sort pseudocode as the Java Source code but fails to work
void sorting(int d[]) {
for (int i = 0; i < sizeof(d); i++) {
for (int j = i + 1; j < sizeof(d); j++) {
if (d[i] > d[j]) {
int temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
}
Input Format
Both methods got parameters from arrays initialized to a fixed size and then a loop was used to get input from user and asign to the array, the user has to enter the size of the array first.
Reliability of the merge sort algo in other languages
I have applied the mergesort pseudocode in JavaScript, Python and C# and they all worked. I do not know why C++ would be an exception, please help...