-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
95 lines (73 loc) · 2.43 KB
/
Copy pathMergeSort.java
File metadata and controls
95 lines (73 loc) · 2.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package Sorting;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] arr = {9, 3, 5, 8, 4, 6, 1};
System.out.println(Arrays.toString(sort(arr)));
}
static int[] sort(int[] arr) {
// If the array has only one element,
// it is already sorted.
if (arr.length == 1)
return arr;
// Find the middle index so we can split the array.
int mid = arr.length / 2;
// Recursively sort the left half.
int[] left = sort(Arrays.copyOfRange(arr, 0, mid));
// Recursively sort the right half.
int[] right = sort(Arrays.copyOfRange(arr, mid, arr.length));
// Merge the two sorted halves into one sorted array.
return merge(left, right);
}
static int[] merge(int[] first, int[] last) {
/*
Compare current elements
↓
Take smaller one
↓
Move that pointer
↓
Repeat
↓
Copy leftovers
*/
// Final array that will store the merged result.
int[] ans = new int[first.length + last.length];
// i -> points to current element in first array
// j -> points to current element in second array
// n -> points to current position in answer array
int i = 0;
int j = 0;
int n = 0;
// Keep comparing elements until one array is exhausted.
while (i < first.length && j < last.length) {
// Put the smaller element into the answer array.
if (first[i] <= last[j]) {
ans[n] = first[i];
i++; // move to next element in first array
} else {
ans[n] = last[j];
j++; // move to next element in second array
}
// Move to next position in answer array.
n++;
}
// If the second array is completely used,
// copy all remaining elements from the first array.
if (j == last.length) {
for (int ele = i; ele < first.length; ele++) {
ans[n] = first[ele];
n++;
}
}
// Otherwise, the first array is exhausted,
// so copy all remaining elements from the second array.
else {
for (int ele = j; ele < last.length; ele++) {
ans[n] = last[ele];
n++;
}
}
return ans;
}
}