forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort
59 lines (50 loc) · 1.33 KB
/
MergeSort
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
import java.util.*;
public class MergeSort{
static void PrintArray(int[] arr){
for(int val :arr){
System.out.print(val + " ");
}
}
static void MergeOverall(int[] arr,int l,int mid,int r){
int n1 = mid-l+1;
int n2 = r - mid;
int[] left = new int [n1];
int[] right = new int [n2];
int i,j,k;
for( i =0; i<n1; i++)
left[i] = arr[l+i];
for( j =0; j<n2; j++)
right[j] = arr[mid+1+j];
i=0;
j=0;
k=l;
while(i<n1 && j<n2){
if(left[i]< right[j]){
arr[k++] = left[i++];
}
else{
arr[k++] = right[j++];
}
}
while(i <n1){
arr[k++] = left[i++];}
while(j<n2){
arr[k++] = right[j++];}
}
static void Merge(int[] arr, int l, int r){
if(l>= r) return;
int mid = (l +r)/2;
Merge(arr,l,mid);
Merge(arr,mid+1,r);
MergeOverall(arr,l,mid,r);
}
public static void main(String[] args) {
int[] arr = {4,1,3,5,2};
int n = arr.length;
System.out.println(" array before sorting");
PrintArray(arr);
Merge(arr,0,n-1);
System.out.println(" array after sorting");
PrintArray(arr);
}
}