Sorting - Merge Sort
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the following −
We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
The Java program :
/* Java program for Merge Sort */
class
MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void
merge(
int
arr[],
int
l,
int
m,
int
r)
{
// Find sizes of two subarrays to be merged
int
n1 = m - l +
1
;
int
n2 = r - m;
/* Create temp arrays */
int
L[] =
new
int
[n1];
int
R[] =
new
int
[n2];
/*Copy data to temp arrays*/
for
(
int
i=
0
; i<n1; ++i)
L[i] = arr[l + i];
for
(
int
j=
0
; j<n2; ++j)
R[j] = arr[m +
1
+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int
i =
0
, j =
0
;
// Initial index of merged subarry array
int
k = l;
while
(i < n1 && j < n2)
{
if
(L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while
(i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while
(j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void
sort(
int
arr[],
int
l,
int
r)
{
if
(l < r)
{
// Find the middle point
int
m = (l+r)/
2
;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+
1
, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static
void
printArray(
int
arr[])
{
int
n = arr.length;
for
(
int
i=
0
; i<n; ++i)
System.out.print(arr[i] +
" "
);
System.out.println();
}
// Driver method
public
static
void
main(String args[])
{
int
arr[] = {
12
,
11
,
13
,
5
,
6
,
7
};
System.out.println(
"Given Array"
);
printArray(arr);
MergeSort ob =
new
MergeSort();
ob.sort(arr,
0
, arr.length-
1
);
System.out.println(
"\nSorted array"
);
printArray(arr);
}
}
Comments
Post a Comment