my code is correct but it is not sorting the array properly
here is my code
Merge sort using Recrsion
@jatinupadhyay786 line 10 j<=e , you have written j==e
dont forget to hit like and mark resolved if cleared
i am not able to understand how merge is getting executed because in the code of function mergeSort it is
i am not able to understand how merge is getting executed because in the code of function mergeSort it is if(s>=e){ return; } so according to this when there is only one elemnt left it is returned out of function so merge cant be executed
can u please explain it to me over call?
@jatinupadhyay786 see this video by prateek bhaiya
https://youtu.be/yzkzQ7oZwIE
you will understand then
(also you should be familiar with recursion for merge sort)
when s==e then 1 element is present so we dont need to sort it as it 1 element is already sorted
bro ek doubt aur tha isliye kia
ha batao kya doubt hai
i am not understanding how merge function is getting called?
pehle hum mid nikal rahe hai
phir ab hamare paa do array aa gayi,
ek s se mid tak aur dusri mid+1 se e tak.
hum ab mergesort in dono array par call kar rahe hai taaki ye dono array sort ho jaye.
ab iske baad jab dono array sort ho ke aa jayegi tab hum merge function call kareneg jo in dono sorted array ko merge kar dega aur ek final sorted array dega.
bus itna hi hai merge sort.
aur merge algorithm same hai merging of two sorted hai.
to tumhe bhi merging algorithm samajhna hai ,
agar abh bhi samaj nahi aaya to video ek baar phir dekho , sayad samjh aa jaye.
ek baar ye recursion tree dekho sayad samajh aa jaye
bro i already know the algorithm understood the code as well the part where i am facing problem is hoe is recursion working here
in the function merge sort its void mergeSort(int a[] , int s , int e){ //Base case if(s>=e){
so if only one elemnt is left then we return out of the function mergeSort then how is merge getting implemented
when there is only one element then no merging will be done.
that is why in base case we have s>=e
when s==e this indicates that our array has only single element so in that case we simply return
still not clear to me i am not understanding hoe merge functionis getting implemented i know the code inside it but how it is getting implemented is not clear to me
bro can u explaing it to me over call because in chat i cant explain you my exact problem i also dry run the code many times but i am still facing problem its like i am not understanding but learning the code
let say we are working on array from index s to e.
now mid will be (s+e)/2.
elements from index s to mid will act as first array (say arr1).
and
elements from index mid+1 to e will act as second array (say arr2).
now our task is to merge this array.
refer this to learn how to merge two sorted array.
sorry bro cant take doubt over call
bhai i know how to merge array but i am not understanding how it is getting called by recursion you are not understandig ny doubt what you are doing is explaing by how to merge i already know that