Merge Sort in Java
The merge sort algorithm is based on the principle of divide and conquer algorithm where a problem is divided into multiple sub-problems. Each sub-problem is solved individually and finally, sub-problems are combined to form the final solutions.
To learn more, visit Merge Sort Algorithm.
Example: Java Program to Implement Merge Sort Algorithm
Output 1
Unsorted Array: [6, 5, 12, 10, 9, 1] Sorted Array: [1, 5, 6, 9, 10, 12]
Here, the elements of the array are sorted in ascending order. If we want to sort the elements in descending order, then inside the first while loop of the merge() method, we can change the code as:
// the less than sign is changed to greater than
if (L[i] >= M[j]) {