-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionFathomHealth.java
More file actions
95 lines (61 loc) · 2.51 KB
/
Copy pathSolutionFathomHealth.java
File metadata and controls
95 lines (61 loc) · 2.51 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class SolutionFathomHealth {
// public static int[] sort(int[] input, int value, int index) {
// for ( int index = 0; index < input.length; index ++ ) {
// if ( value < input[index] ) {
// int tmp = input[index];
// input[index] = value;
// }
// }
// }
public static int[] merge(int[] a, int[] b) {
int[] output = new int[a.length + b.length];
int innerIndex = 0;
int index = 0;
int endIndex = a.length <= b.length ? b.length : a.length;
// a = 1 b = 2 output = 1, 2
// a = 4 b = 6 output = 1, 2, 4, 6
// a = 3 b = 7 output = 1, 2, 4, 6, 3, 7
while ( index < endIndex ) {
if ( a.length > index && b.length > index ) {
if ( a[index] <= b[index] ) {
output[innerIndex] = a[index];
output[innerIndex + 1] = b[index];
} else {
output[innerIndex] = b[index];
output[innerIndex + 1] = a[index];
}
innerIndex = innerIndex + 2;
} else {
if ( a.length < index ) {
output[innerIndex] = b[index];
} else {
output[innerIndex] = a[index];
}
innerIndex++;
}
index++;
}
return output;
}
// public static int[] mergeSort(int[] array) {
// // int[] result = new int[array.length];
// // int middleIndex = array.length / 2;
// // bool finished = false;
// // int index = 0;
// // while (!finished) {
// // if ( )
// // }
// // for ( int index; index < array.length; index++ ) {
// // }
// }
public static void main(String args[] ) throws Exception {
int[] arrays = new int[] {4, 5, 7, 1, 3, 4};
System.out.println(Arrays.toString( merge(new int[]{1, 4, 5}, new int[]{2, 6, 7, 8}))); // expect 1, 2, 4, 5, 6, 7, 8
System.out.println("Hello World");
}
}