-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
42 lines (36 loc) · 963 Bytes
/
MergeSort.java
File metadata and controls
42 lines (36 loc) · 963 Bytes
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
public class Solution {
public static void mergeSort(int[] arr, int l, int r){
// Write your code here
if(l == r)
{
return;
}
int mid = (l+r)/2;
mergeSort(arr, l, mid);
mergeSort(arr, mid+1, r);
merge(arr, mid, l, r);
}
public static void merge(int arr[], int mid, int low, int high) {
int left = low;
int right = mid + 1;
int[] temp = new int[high - low + 1]; // Create a temporary array to store merged values
int index = 0;
while (left <= mid && right <= high) {
if (arr[left] < arr[right]) {
temp[index++] = arr[left++];
} else {
temp[index++] = arr[right++];
}
}
while (left <= mid) {
temp[index++] = arr[left++];
}
while (right <= high) {
temp[index++] = arr[right++];
}
// Copy elements from temp array back to original array
for (int i = low; i <= high; i++) {
arr[i] = temp[i - low];
}
}
}