+ 2
Debugging C++
I was writing a code that combines Merge Sort with Insertion Sort. It implements Insertion Sort when the array to be sorted is divided into less or equal to log(N) elements. However, the output came out buggy. Can anyone help me to find what's wrong with my code here? https://code.sololearn.com/cvxzMo7xJtvH/?ref=app
10 Answers
+ 5
The loop in insertion sort should iterate from l+1 to r
for(int i = l + 1; i < r+ 1; i++)
+ 2
It seems you forgot to put k++ in the two loops at the end of merge function. The index to arr should be increased as you add new elements. If the problem persists, please give an input which causes wrong results.
+ 2
you're welcome
+ 1
I added k++ at the end of merge function now. Unfortunately, the problem persisted. :(
There are a lot of inputs that are causing problems while strange enough, some of the inputs work.
I tried the following input:
5
3 4 7 9 1
It gives me the output:
3 4 7 9 1
+ 1
Yep, the code worked! Thanks for helping.
+ 1
I think you can post a new topic for this
0
Thanks for the information! Does that mean in general insertion sort beats merge sort if we use dynamic programming i.e using cache?
0
Thanks! Does ns mean nanoseconds? Also, what does el. mean?
0
Thanks for your help.
0
Hi
I made a c++ calculator in pc
Its working there
But not here
And it has no problem
See if you can help me fix it
Thanks