+ 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

9th Mar 2019, 3:25 PM
Ah Zong
Ah Zong - avatar
10 odpowiedzi
+ 5
The loop in insertion sort should iterate from l+1 to r for(int i = l + 1; i < r+ 1; i++)
10th Mar 2019, 11:15 AM
michal
+ 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.
9th Mar 2019, 5:17 PM
michal
+ 2
you're welcome
10th Mar 2019, 1:11 PM
michal
+ 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
10th Mar 2019, 2:09 AM
Ah Zong
Ah Zong - avatar
+ 1
Yep, the code worked! Thanks for helping.
10th Mar 2019, 11:37 AM
Ah Zong
Ah Zong - avatar
+ 1
I think you can post a new topic for this
14th Jun 2019, 8:18 AM
Ah Zong
Ah Zong - avatar
0
Thanks for the information! Does that mean in general insertion sort beats merge sort if we use dynamic programming i.e using cache?
10th Mar 2019, 3:10 PM
Ah Zong
Ah Zong - avatar
0
Thanks! Does ns mean nanoseconds? Also, what does el. mean?
11th Mar 2019, 7:45 AM
Ah Zong
Ah Zong - avatar
0
Thanks for your help.
11th Mar 2019, 10:40 AM
Ah Zong
Ah Zong - avatar
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
14th Jun 2019, 8:11 AM
Mamad reza Sheylani
Mamad reza Sheylani - avatar