forked from iRupam/NewtonSchoolInfinityJune21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortExample.java
More file actions
79 lines (65 loc) · 2 KB
/
MergeSortExample.java
File metadata and controls
79 lines (65 loc) · 2 KB
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
69
70
71
72
73
74
75
76
77
78
79
package InfinityJune21.MergeSort;
public class MergeSortExample {
static void printArray(int arr[]) {
int size = arr.length; //finding size of the array
for(int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
static void sort(int arr[], int low, int high) {
if(low < high) {
int middle = (low + high) / 2;
sort(arr, low, middle);
sort(arr, middle + 1, high);
merge(arr, low, middle, high);
}
}
static void merge(int[] arr, int low, int middle, int high) {
//size of the left and right arrays
int size1 = middle - low + 1;
int size2 = high - middle;
//declaring the two arrays
int left[] = new int[size1];
int right[] = new int[size2];
//initializing the two arrays
for(int i = 0; i < size1; i++) {
left[i] = arr[low + i];
}
for(int i = 0; i < size2; i++) {
right[i] = arr[middle + 1 + i];
}
int i = 0, j = 0, k = low;
while(i < size1 && j < size2) {
if(left[i] <= right[j]) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}
while(i < size1) {
arr[k] = left[i];
i++;
k++;
}
while(j < size2) {
arr[k] = right[j];
j++;
k++;
}
}
public static void main(String[] args) {
int arr[] = {23, 18, 29, 30, 10, 49, 99, 45};
int low = 0;
int high = arr.length - 1;
System.out.println("Original Array: ");
printArray(arr); //printing the array
sort(arr, low, high);
System.out.println();
System.out.println("Sorted Array: ");
printArray(arr); //printing the array
}
}