First of all, Why do we need to learn so many sorting algorithms?
We need to learn so many sorting algorithms because there are cases in which which algorithm fits best. According to the time complexity, we have to choose an algorithm of our use. That's why we are learning so many sorting algorithms.
SO LETS START WITH THE MERGE SORT
In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. It is one of the most popular algorithms and one of the most stable sorting algorithm.
ALGORITHM
1.) First of all take a list and put it in a function (recursion) that it breaks itself into two halves.
2.) When we get the two halves, the recursion code will automatically breaks the two halves into four parts and then into 8 parts until each list contains a single element.
3.) Then compare the first element with the next element and put the smaller one into our original list and compare the next element with it and put in the original list accordingly.
4.) Now run a loop to find if some element left uncompared. Put them in the original list accordingly.
5.) Run a print function to print all the elements.
Comments
Post a Comment