How does MergeSort algorithm works? | Sololearn: Learn to code for FREE!

How does MergeSort algorithm works?

It is said that merge function doesn't occure until array isn't divided on sigle elements. But how can all elements in array be sorted when then size is 1? You ony compare the first two elemnts of array. But doesn't Merge function occurs every time recursion is called. I analysed it in comment in code, but my analysis isn't correct.

23rd Dec 2021, 5:07 PM
1 Answer
+ 2
I think the best way for you to analyse this is to add exactly the output the you wrote in the comments below your code 🙂 Just add some cout's before you call functions and/or when you step in, print values of local variables and parameters, and trace back how that thing works 🍀🙂
23rd Dec 2021, 7:49 PM
Agnes Ahlberg
Agnes Ahlberg - avatar