-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombineAndSortArrays.java
155 lines (145 loc) · 5.96 KB
/
CombineAndSortArrays.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.sparta.crss;
import java.util.Arrays;
//import java.util.ArrayList;
import java.util.List;
public class CombineAndSortArrays {
// public int[] CombineAndSortArrays(int[] inputArray1, int[] inputArray2) {
// int[] combinedArray = new int[inputArray1.length + inputArray2.length];
// System.arraycopy(inputArray1, 0, combinedArray, 0, inputArray1.length);
// System.arraycopy(inputArray2, 0, combinedArray, inputArray1.length, inputArray2.length);
// //System.out.println(Arrays.toString(combinedArray));
// Arrays.sort(combinedArray);
// return combinedArray;
// }
public int[] CombineSortedArrays(int[] inputArray1, int[] inputArray2) {
// checking arrays are sorted - half-finished
// int[] sortedInputArray1 = inputArray1;
// Arrays.sort(sortedInputArray1);
// int[] sortedInputArray2 = inputArray2;
// Arrays.sort(sortedInputArray2);
// System.out.println(Arrays.toString(inputArray1));0
// System.out.println(Arrays.toString(sortedInputArray1));
// return inputArray1;
int[] resultArray = new int[inputArray1.length + inputArray2.length];
//ArrayList<int> resultArrayList = new ArrayList<int>();
//List<int> resultList = new List<int>();
int resultArrayIndex = 0;
// for (int i = 0; i < inputArray1.length; i++) {
// for (int j = 0; j < inputArray2.length; j++) {
// if (inputArray1[i] < inputArray2[j]) {
// resultArray[resultArrayIndex] = inputArray1[i];
// } else {
// resultArray[resultArrayIndex] = inputArray2[j];
// }
// resultArrayIndex++;
// }
// }
int inputArray1Index = 0;
int inputArray2Index = 0;
// while (inputArray1Index < inputArray1.length && inputArray2Index < inputArray2.length && resultArrayIndex < resultArray.length) {
// while (inputArray1Index < inputArray1.length || inputArray2Index < inputArray2.length || resultArrayIndex < resultArray.length) {
// while (resultArrayIndex < resultArray.length) {
while (inputArray1Index < inputArray1.length && inputArray2Index < inputArray2.length) {
if (inputArray1[inputArray1Index] < inputArray2[inputArray2Index]) {
resultArray[resultArrayIndex] = inputArray1[inputArray1Index];
resultArrayIndex++;
inputArray1Index++;
} else {
resultArray[resultArrayIndex] = inputArray2[inputArray2Index];
resultArrayIndex++;
inputArray2Index++;
}
}
// adds the rest of the remaining array
if (inputArray1Index < inputArray1.length) {
for (int i = 0; i < inputArray1.length - inputArray1Index; i++) {
resultArray[resultArrayIndex] = inputArray1[inputArray1Index + i];
resultArrayIndex++;
}
}
if (inputArray2Index < inputArray2.length) {
for (int i = 0; i < inputArray2.length - inputArray2Index; i++) {
resultArray[resultArrayIndex] = inputArray2[inputArray2Index + i];
resultArrayIndex++;
}
}
// int i = 0;
// while (inputArray1Index < inputArray1.length) {
// resultArray[resultArrayIndex] = inputArray1[inputArray1Index + i];
// i++;
// }
// int j = 0;
// while (inputArray2Index < inputArray2.length) {
// resultArray[resultArrayIndex] = inputArray2[inputArray2Index + j];
// j++;
// }
return resultArray;
}
//public int[] mergeSort(int[] inputArray) {
//steps:
//check if length is more than two
//if yes, split
//repeat etc.
//if it is one, leave it
//if it is two, check which number is bigger
//if the left is larger, swap
//int inputArrayLength = inputArray.length;
//int[][] sortedArraysArray;
//if ()
//}
public int[] divideArray(int[] inputArray) {
//int[][] arrayInHalf = new int[][];
int inputArrayLength = inputArray.length;
if (inputArrayLength > 2) {
int midpoint = inputArrayLength / 2;
int[] left = new int[midpoint];
int[] right = new int[inputArrayLength - midpoint];
for (int i = 0; i < midpoint; i++) {
left[i] = inputArray[i];
}
for (int i = midpoint; i < inputArrayLength; i++) {
right[i - midpoint] = inputArray[i];
}
if (left.length < 3 && right.length < 3) {
int[] sortedLeft = sortTwoNumbers(left);
int[] sortedRight = sortTwoNumbers(right);
int[] combinedArrays = CombineSortedArrays(sortedLeft, sortedRight);
return combinedArrays;
} else {
//TODO: come back to this
// int[] placeholder = {1, 2};
// return placeholder;
int[] newLeftArray = divideArray(left);
int[] newRightArray = divideArray(right);
return CombineSortedArrays(newLeftArray, newRightArray);
//for testing purposes:
//return newRightArray;
}
} else {
return sortTwoNumbers(inputArray);
}
}
public int[] sortTwoNumbers(int[] array) {
if (array.length == 1) {
return array;
} else if (array.length == 2) {
int temp = 0;
if (array[1] < array[0]) {
temp = array[0];
array[0] = array[1];
array[1] = temp;
}
}
return array;
}
public int[] mergeSort(int[] inputArray) {
int inputArrayLength = inputArray.length;
if (inputArrayLength > 2) {
return divideArray(inputArray);
}
//?
else {
return inputArray;
}
}
}