-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.java
110 lines (99 loc) · 2.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package codingProblems;
import java.io.*;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) throws IOException{
BufferedReader R = new BufferedReader(new InputStreamReader(System.in));
int arraySize = Integer.parseInt(R.readLine());
int[] inputArray = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
inputArray[i] = Integer.parseInt(R.readLine());
}
int[] inputArray2=inputArray;
mergeSortTopDown(inputArray);
for (int j = 0; j < inputArray.length; j++) {
System.out.print(inputArray[j]+" ");
}
System.out.println();
sort(inputArray2);
for (int j = 0; j < inputArray2.length; j++) {
System.out.print(inputArray2[j]+" ");
}
}
//top-down
static void mergeSortTopDown(int[] A) {
if (A.length > 1) {
int q = A.length/2;
int[] leftArray = Arrays.copyOfRange(A, 0, q);
int[] rightArray = Arrays.copyOfRange(A,q,A.length);
mergeSortTopDown(leftArray);
mergeSortTopDown(rightArray);
merge(A,leftArray,rightArray);
}
}
static void merge(int[] a, int[] l, int[] r) {
int i,li,ri;
i = li = ri = 0;
while ( i < l.length + r.length) {
if ((li < l.length) && (ri<r.length)) {
if (l[li] < r[ri]) {
a[i] = l[li];
i++;
li++;
}
else {
a[i] = r[ri];
i++;
ri++;
}
}
else {
if (li >= l.length) {
while (ri < r.length) {
a[i] = r[ri];
i++;
ri++;
}
}
if (ri >= r.length) {
while (li < l.length) {
a[i] = l[li];
li++;
i++;
}
}
}
}
}
//Bottom-up
public static void sort(int[] array) {
int[] workArray = new int[array.length];
int chunkSize = 1;
while (chunkSize < array.length) {
int i = 0;
while (i < array.length - chunkSize) {
merge(array, i, chunkSize, workArray);
i += chunkSize * 2;
}
chunkSize *= 2;
}
}
public static void merge(int[] array, int leftPosition, int chunkSize, int[] workArray) {
int rightPosition = leftPosition + chunkSize;
int endPosition = Math.min(leftPosition + chunkSize * 2 - 1, array.length - 1);
int leftIndex = leftPosition;
int rightIndex = rightPosition;
for (int i = 0; i <= endPosition - leftPosition; i++) {
if (leftIndex < rightPosition &&
(rightIndex > endPosition ||
array[leftIndex] <= array[rightIndex])) {
workArray[i] = array[leftIndex++];
} else {
workArray[i] = array[rightIndex++];
}
}
for (int i = leftPosition; i <= endPosition; i++) {
array[i] = workArray[i - leftPosition];
}
}
}