Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed.
Merge sort is a sorting method that uses the divide and conquer method. It is one of the most respected algorithms, with a worst-case time complexity of O(n log n). Merge sort splits the array into equal halves before combining them in a sorted order.
Merge sort divides the list into equal halves until it can't be divided any further.
If there is only one element in the list, it is sorted by definition.
Merge sort then joins the smaller sorted lists together, keeping the resultant list sorted as well.
Step 1 − if it is only one element in the list it is already sorted, return. Step 2 − divide the list recursively into two halves until it can no more be divided. Step 3 − merge the smaller lists into new list in sorted order.
MergeSort(arr, l, r) If r > l 1. Find the middle point to divide the array into two halves: middle m = l+ (r-l)/2 2. Call mergeSort for first half: Call mergeSort(arr, l, m) 3. Call mergeSort for second half: Call mergeSort(arr, m+1, r) 4. Merge the two halves sorted in step 2 and 3: Call merge(arr, l, m, r)
Java is the most distressing thing to hit computing since MS-DOS.Alan Kay