-
Notifications
You must be signed in to change notification settings - Fork 368
/
merge_sort.java
67 lines (63 loc) · 1.92 KB
/
merge_sort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Scanner;
public class merge_sort {
public void merge(int[] arr, int l, int mid, int r){
int i = l, j = mid+1, k=0;
int[] b = new int[r-l+1];
// Compares the elements of the two arrays and sorts them
while (i<=mid && j<=r)
{
if (arr[i]>arr[j]){
b[k] = arr[j];
j++;
k++;
}
else{
b[k] = arr[i];
i++;
k++;
}
}
// If the 1st array is exhausted, then copies the remaining elements of the 2nd array to the 1st array
while (j<=r){
b[k] = arr[j];
j++;
k++;
}
// If the 2nd array is exhausted, then copies the remaining elements of the 1st array to the 2nd array
while (i<=mid){
b[k] = arr[i];
i++;
k++;
}
// Copies all the elements of the array 'b' to array 'arr'
for (int x = l; x <= r; x++){
arr[x] = b[x-l];
}
}
// Function that divides the array into half at each step and sorts them
void mergeSort(int[] arr, int l, int r){
if (l<r)
{
int mid = l + (r-l)/2;
// Sorts the left array
mergeSort(arr, l, mid);
// Sorts the right array
mergeSort(arr, mid+1, r);
// Merges the left and the right array
merge(arr, l, mid, r);
}
}
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = s.nextInt();
// function call
merge_sort hs = new merge_sort();
hs.mergeSort(arr, 0, n-1);
// display sorted array
System.out.print("After sorting:\n");
for(int i : arr) System.out.print(i + " ");
}
}