For a Programmer sorting is an easy task after writing the code for couple of pages. But there are many other sorting algorithms.
The question which is not being properly answered is which sorting algorithm you should choose.
This is the most frustrating situation where we feel bad about us. The best sorting algorithm depends only on the scenario in which you are intended to use.
Let us speak about the Merge sort.
- Merge sort uses the divide and conquer approach.
- Entirely the merge sort is an easy algorithm (Depends on the way you think).
- You cannot master merge sort until you think what is happening.
- It has a good worst case performance (0(N logN)).
- It uses an additional space to accomplish the goal.
- The implementation is mainly based on the recursion.
- You can get real performance benefit when comparing to bubble,selection sort.
That's enough for the quick intro. If you would like you know more do a research on yourself.
The algorithm goes like this:
- Take the array and divide it until you get single element.
- Each array will yield two sub-arrays. Each sub-array will give two and goes on..
- Now at the end you will get only single elements.
- All the single elements are again combined in the same way while ensuring the elements are in the proper order.
- Finally you will get the sorted array as a present.
Don't panic looking at the algorithm, i will ensure good amount of comments n my code.
Just look at the image to get good view of what is happening.
Just looking at the image is not sufficient. Get your hands dirty by working with pen and paper.
Don't get surprised about the failure. Do it again and again until you get success.
The code goes here...
Try to code on your own instead of doing a copy paste. Also remember the mantra for success.
CREDIT: HackerEarth.com(Code monk) for the stuff and the image.
Comments
Post a Comment