Why I am getting all zeroes as answer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why I am getting all zeroes as answer

Merge Sort https://code.sololearn.com/cSYoGofqmp0q/?ref=app

5th Jun 2022, 10:29 AM
Viraj
Viraj - avatar
9 Answers
+ 2
WeRaj 👑 In your original code, you need first like assign values in part1, part2 as for(int n1=0; n<part1.length; n++) part1[n1] = input[l+n1]; for(int n2=0; n2<part2.length; n2++) part2[n2] = input[m+1+n2]; Here m is mid index value, l is lower bound which you are missing to take into count. Add this before whole loop... There you need some more changes I think... Hope this helps to correct remaining ...
5th Jun 2022, 3:14 PM
Jayakrishna 🇮🇳
+ 2
You are creating these two arrays int[] part1 = new int[mid]; int[] part2 = new int[input.length-mid]; Which are initialized to 0s initially, and you are assigning these array values into input[] array.. All you need is like : part1[k] = input[j]; instead of input[k] = part1[j];. You are doing reverse assigning.. also first assign then apply sort. Hope it helps....
5th Jun 2022, 10:49 AM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 sorry I forgot 😅🥲
5th Jun 2022, 4:55 PM
Viraj
Viraj - avatar
+ 1
Jayakrishna🇮🇳 it's not working bro after doing suggested changes i am getting same array
5th Jun 2022, 10:59 AM
Viraj
Viraj - avatar
+ 1
Can you save the changes that Jayakrishna🇮🇳 suggested into the code here? At the moment it's still showing the same. I did all 4 changes and the code ran with no problems.
5th Jun 2022, 11:07 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Ausgrindtube Jayakrishna🇮🇳 see it's not working array is not sorted
5th Jun 2022, 12:06 PM
Viraj
Viraj - avatar
+ 1
The code hasn't been updated. This is what you have. if(part1[i]<part2[j]) { input[k]=part1[i]; k++; i++; } else { input[k]=part2[j]; k++; j++; } } while(i<part1.length) { input[k]=part1[i]; k++; i++; } while(j<part2.length){ input[k]=part2[j]; k++; j++; } } This is what Jayakrishna🇮🇳 said to do. if(part1[i]<part2[j]) { part1[i]= input[k]; k++; i++; } else { part2[j]= input[k]; k++; j++; } } while(i<part1.length) { part1[i]= input[k]; k++; i++; } while(j<part2.length){ part2[j]= input[k]; k++;
5th Jun 2022, 12:50 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
WeRaj 👑 actually you have incorrect implementation of Mergesort algorithm. In algorithm, you need to split into parts of single valued and then merge those into a sorted order compare to original.. but you are dividing into 2 parts.. Creating array in merger function will create a new empty array each time.. You are dealing with empty array then. My suggestion just give you assign correct way but that does not involving in sorting array... Once read again algorithm then give another try... Hope it helps...
5th Jun 2022, 3:00 PM
Jayakrishna 🇮🇳
0
Ausgrindtube just try to run the code after making changes still it doesn't give required output
5th Jun 2022, 2:31 PM
Viraj
Viraj - avatar