-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
260 lines (217 loc) · 7.79 KB
/
Sorting.java
File metadata and controls
260 lines (217 loc) · 7.79 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import java.util.ArrayList;
import java.util.Arrays;
public class Sorting {
public static void main(String[] args) {
int[][] arr = new int[][]{{15, 20, 40, 85}, {20, 35, 80, 95}};
matrixSort(arr, 40);
}
//mergesort O(nlogn)
public static void mergeSort(int[] array) {
int[] helper = new int[array.length];
mergeSort(array, helper, 0, array.length - 1);
}
public static void mergeSort(int[] array, int[] helper, int low, int high) {
if (low < high) {
int middle = low + (high - low) / 2;
mergeSort(array, helper, low, middle);
mergeSort(array, helper, middle + 1, high);
merge(array, helper, low, middle, high);
}
}
private static void merge(int[] array, int[] tempArray, int low, int middle, int high) {
for (int i = low; i <= high; i++) {
tempArray[i] = array[i];
}
int left = low;
int right = middle + 1;
int current = low;
while (left <= middle && right <= high) {
if (tempArray[left] <= tempArray[right]) {
array[current] = tempArray[left];
left++;
} else {
array[current] = tempArray[right];
right++;
}
current++;
}
while (left <= middle) {
array[current] = tempArray[left];
current++;
left++;
}
}
//mergesort O(nlogn)
public static void quickSort(int[] array) {
quickSort(array, 0, array.length - 1);
}
public static void quickSort(int[] array, int low, int high) {
if (low > high) return;
int index = partition(array, low, high);
if (low < index - 1) quickSort(array, low, index - 1);
if (index < high) quickSort(array, index, high);
}
public static int partition(int[] array, int low, int high) {
int pivot = array[(low + high) / 2];
while (low <= high) {
while (array[high] > pivot) high--;
while (array[low] < pivot) low++;
if (low <= high) {
swap(array, low, high);
low++;
high--;
}
}
return low;
}
public static void swap(int[] array, int low, int high) {
int temp = array[low];
array[low] = array[high];
array[high] = temp;
}
//binary search (but just use Arrays.binarySearch)
public static int binarySearch(int[] array, int x) {
int low = 0;
int high = array.length - 1;
int mid;
while (low <= high) {
mid = (low + high) / 2;
if (array[mid] < x) low = mid + 1;
else if (array[mid] > x) high = mid - 1;
else return mid;
}
return -1; //error
}
public static int binarySearch_rec(int[] array, int x) {
return binarySearch_rec(array, x, 0, array.length - 1);
}
public static int binarySearch_rec(int[] array, int x, int low, int high) {
if (low > high) return -1;
int mid = (low + high) / 2;
if (x < array[mid]) return binarySearch_rec(array, x, low, mid - 1);
else if (x > array[mid]) return binarySearch_rec(array, x, mid + 1, high);
else return mid;
}
//11.1
//merge 2 arrays where first has room for second (sorted)
public static void mergeArrays(int[] arr, int[] arr2) {
mergeArrays(arr, arr2, arr.length - arr2.length, arr2.length);
}
private static void mergeArrays(int[] arr, int[] arr2, int last, int last2) {
int index = last - 1; //last element in arr
int index2 = last2 - 1; //last element in arr2
int indexMerged = last2 + last - 1; //end of merged array
//merge arr and arr2 starting from the last element in each
while (index >= 0 && index2 >= 0) {
//end of arr is > end of arr2
if (arr[index] > arr2[index2]) {
arr[indexMerged] = arr[index]; //copy
indexMerged--; //move indices
index--;
} else {
arr[indexMerged] = arr2[index2];
indexMerged--;
index2--;
}
}
//copy remaining elements from b into place
while (index2 >= 0) {
arr[indexMerged] = arr2[index2];
indexMerged--;
index2--;
}
}
//merge two unsroted arrays
void mergeUnsorted(int mPlusN[], int N[], int m, int n) {
int i = n;
/* Current index of i/p part of mPlusN[]*/
int j = 0;
/* Current index of N[]*/
int k = 0;
/* Current index of of output mPlusN[]*/
while (k < (m + n)) {
/* Take an element from mPlusN[] if
a) value of the picked element is smaller and we have
not reached end of it
b) We have reached end of N[] */
if ((i < (m + n) && mPlusN[i] <= N[j]) || (j == n)) {
mPlusN[k] = mPlusN[i];
k++;
i++;
} else // Otherwise take element from N[]
{
mPlusN[k] = N[j];
k++;
j++;
}
}
}
//move nulls (-1) to end of array
void moveToEnd(int mPlusN[], int size) {
int i, j = size - 1;
for (i = size - 1; i >= 0; i--) {
if (mPlusN[i] != -1) {
mPlusN[j] = mPlusN[i];
j--;
}
}
}
//11.3
//search a sorted rotated array for a value
//use binary search but altered
//O(logn) time if all elements are unique, O(N) if duplicates
public static int rotatedBinarySearch(int[] arr, int left, int right, int x) {
int mid = (left + right) / 2;
if (x == arr[mid]) return mid;
if (right < left) return -1;
if (arr[left] < arr[mid]) { //left normally ordered
if (x >= arr[left] && x <= arr[mid])
return rotatedBinarySearch(arr, left, mid - 1, x); //search left;
else
return rotatedBinarySearch(arr, mid + 1, right, x); //search right;
} else if (arr[mid] < arr[left]) { //right normally ordered
if (x >= arr[mid] && x <= arr[right])
return rotatedBinarySearch(arr, mid + 1, right, x); //search right;
else
return rotatedBinarySearch(arr, left, mid - 1, x); //search left;
} else if (arr[left] == arr[mid]) { //left is all repeats
if (arr[mid] != arr[right]) //if right is diff. search it
return rotatedBinarySearch(arr, mid + 1, right, x); //search right;
else {
int result = rotatedBinarySearch(arr, left, mid - 1, x); //search left;
if (result == -1)
return rotatedBinarySearch(arr, mid + 1, right, x); //search right;
else return result;
}
}
return -1;
}
//11.6
//sort a matrix with columns and rows sorted
//O(n)
public static boolean matrixSort(int[][] matrix, int x) {
int row = 0;
int col = matrix[0].length - 1;
while (row < matrix[0].length && col >= 0) {
if (matrix[row][col] == x) {
System.out.println(row + ", " + col);
return true;
} else if (matrix[row][col] > x)
col--;
else
row++;
}
return false;
}
//O(nlogm)
public static boolean matrixSortSimple(int[][] matrix, int x) {
for (int i = 0; i < matrix.length; i++) {
int index = Arrays.binarySearch(matrix[i], x);
if (index > 0) {
System.out.println(i + " " + index);
return true;
}
}
return false;
}
}