-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortInPlace.java
61 lines (46 loc) · 1.39 KB
/
MergeSortInPlace.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
//TC: O(n^2 logn) since merge takes n^2
//SC: O(1)
import java.util.*;
public class MergeSortInPlace {
public static void mergeSort(int[] arr, int l, int r) {
if (l == r)
return;
int m = l + (r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
public static void merge(int[] arr, int start, int mid, int end) {
int start2 = mid+1;
if (arr[mid] <= arr[start2])
return;
while (start <= mid && start2 <= end) {
if (arr[start] <= arr[start2])
start++;
else {
int temp = arr[start2];
int index = start2;
while (index != start) {
arr[index] = arr[index-1];
index--;
}
arr[start] = temp;
start++;
mid++;
start2++;
}
}
}
public static void main(String[] args) {
int[] arr = {45, 12, 34, 12, 1};
System.out.println("Array before sorting:");
for (int x: arr)
System.out.print(x + " ");
System.out.println();
mergeSort(arr, 0, arr.length-1);
System.out.println("Array after sorting:");
for (int x: arr)
System.out.print(x + " ");
System.out.println();
}
}