-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeSort.java
executable file
·68 lines (54 loc) · 1.52 KB
/
MergeSort.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
68
import java.util.Arrays;
// It divides the input array into two halves, calls itself and divides again,
// Once the arrays are divided into smaller arrays such that (l=r),
// then merge() function gets called which merges smaller arrays in sorted order.
class MergeSort{
public static void main(String[] args){
int arr[]={8,5,2,9,0,0,12,7};
sort(arr,0,arr.length-1); //pointers never point to arrays.length, it'll give array index of out of bounds
System.out.println(Arrays.toString(arr));
}
static void sort(int[] arr,int l,int r){
int mid=(l+r)/2;
if(l<r){ // when l==r, size of array is 1
sort(arr,l,mid); // left part of array
sort(arr,mid+1,r); // right part of array
merge(arr,l,r,mid); // merge the subarrays
}
}
static void merge(int arr[],int l,int r,int mid){
int l_size= mid-l+1; // size of left array
int r_size= r-mid; // size of right array
int l_arr[] = new int[l_size];
int r_arr[] = new int[r_size];
int i,j,k; //Copying of element starts from l
for(i=0;i<l_size;i++) //copying the elements of main array to subarray
l_arr[i]=arr[l+i];
for(j=0;j<r_size;j++)
r_arr[j]=arr[j+mid+1];
i=0;j=0;
//pasting of elements starts from from l k=l
k=l; // l is initial index of merged array
while(i<l_size && j<r_size){
if(l_arr[i]<= r_arr[j]){
arr[k]=l_arr[i];
i++;
}
else {
arr[k]=r_arr[j];
j++;
}
k++;
}
while(i<l_size){
arr[k]=l_arr[i];
i++;
k++;
}
while(j<r_size){
arr[k]=r_arr[j];
j++;
k++;
}
}
}