15,741,902 members
See more:
My function always prints unsorted elements, I've searched so much and my code looks exactly like to ones working perfectly. Can anyone help me and tell me where is the problem?

What I have tried:

Main function

```int main(){
int size;
cin >> size;
int* array = new int[size];
for (int i = 0; i < size; i++)
{
cin >> array[i];
}

mergesort(array, 0, size - 1);

for (int i = 0; i < size; i++)
{
cout << "-" << array[i] << endl;
}

system ("pause");
return false;
}```

Merge sort function

```//merge sort function
void mergesort(int arr[], int start , int end){
//base case
if(start < end){
int mid = start + (end - start) / 2;

// recursive call
mergesort(arr, start, mid);
mergesort(arr, ++mid, end);

merge(arr, start, mid , end);
}
}

//merge function
void merge(int arr[], int start, int mid, int end){
//size of sub-arrays - done
int l_size = mid - start + 1;
int r_size = end - mid;
//sub-arrays - done
int* larr = new int[l_size];
int* rarr = new int [r_size];
//fill the sub-arrays - done
for (int i = 0; i < mid + 1; i++)
larr[i] = arr[i + start];
// int x = 0;
for (int i = 0; i < end + 1; i++)
rarr[i] = arr[i + mid + 1];

//indexes - done
static int index1 = 0, index2 = 0, index3 = start;
//sorting algo - done
while (index1 < l_size && index2 < r_size)
{
if(larr[index1] < rarr[index2])
arr[index3++] = larr[index1++];
else
arr[index3++] = rarr[index2++];
}

//rest of the elements - done
while (index1 < l_size)
arr[index3++] = larr[index1++];

while (index2 < r_size)
arr[index3++] = rarr[index2++];

}```
Posted
Updated 25-Jun-22 7:41am
v2
Patrice T 25-Jun-22 7:26am
Complete the code to include a main that show the problem.
include sample data.
Abdullah Yahya 2022 25-Jun-22 9:21am
OK

## Solution 1

Maybe someone will help with this. But if they do, you won't have to learn how to use a debugger, which is the only reasonable way to find problems in non-trivial code. A debugger lets you step through your code line by line as it executes, so that you can look at the current value of its variables to see where its behavior deviates from what you expected. If you don't want to ask where your problem is every time you can't find it with pencil and paper, you must learn to use a debugger. It's as simple as that.

Abdullah Yahya 2022 25-Jun-22 9:21am
Abdullah Yahya 2022 25-Jun-22 11:13am
I've figured out the problem while going step by step on the debugger. Thanks a lot!!!!
Greg Utas 25-Jun-22 12:16pm
Well done! You have learned one of the most important skills that a developer needs.

## Solution 2

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Abdullah Yahya 2022 25-Jun-22 9:21am

## Solution 3

I think you're doing this the hard way. Here is an easier way:

C++
```std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));```

Abdullah Yahya 2022 25-Jun-22 9:57am
Yeah, that's way easier and more efficient. But, I'm studying data structures and algorithms, and this is an assignment. It's for study purposes only, just to understand how it works. And thank you very much.

## Solution 4

First of all, your code can't be run as is, part is missing. We have to make guesses to have complete code, and this is not good for debugging.
I would change the main to :
C++
```int main(){
int size=7;
// cin >> size;
// int* array = new int[size];
// for (int i = 0; i < size; i++)
// {
//     cin >> array[i];
// }
int* array = {1, 5, 7, 6, 8, 4, 3};
mergesort(array, 0, size - 1);

for (int i = 0; i < size; i++)
{
cout << "-" << array[i] << endl;
}

system ("pause");
return false;
}```

So that just running the code will exhibit the problem, no input from us.

I have doubt on :
C++
```void mergesort(int arr[], int start , int end){
//base case
if(start < end){
int mid = start + (end - start) / 2;

// recursive call
mergesort(arr, start, mid);
mergesort(arr, ++mid, end); // I would use mid+1 here
// because the ++mid changes
merge(arr, start, mid , end); // the mid here
}
}```

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Abdullah Yahya 2022 26-Jun-22 4:05am
Thank you very much Patrice! I've followed the debugger and figured it out.
Patrice T 26-Jun-22 5:50am
Nice to hear it.
If the solution was useful and helped you to solve the problem, you can accept the solution.
Patrice T 26-Jun-22 6:18am
Keep using the debugger and you will learn big.
Abdullah Yahya 2022 26-Jun-22 7:52am
Thank you very much!