-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursionI.java
182 lines (168 loc) · 5.07 KB
/
RecursionI.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
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
public class RecursionI {
public static void main(String[] args) {
RecursionI r = new RecursionI();
int[] array = new int[] { 3, 5, 1, 2, 4, 8 };
r.mergeSort(array);
}
/**
* a to the power of b.
* <p>
* Evaluate a to the power of b, assuming both a and b are integers and b is
* non-negative.
*/
public long power(int a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long temp = power(a, b / 2);
return temp * temp * power(a, b % 2);
}
/**
* Selection Sort
* <p>
* an array of integers, sort the elements in the array in ascending order. The
* selection sort algorithm should be used to solve this problem.
*/
public int[] solve(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
swap(array, i, minIndex);
}
return array;
}
/**
* merge sort
* <p>
* Given an array of integers, sort the elements in the array in ascending
* order. The merge sort algorithm should be used to solve this problem.
*/
public int[] mergeSort(int[] array) {
if (array == null || array.length == 0) {
return array;
}
mergeSort(array, 0, array.length - 1);
return array;
}
private void mergeSort(int[] array, int left, int right) {
if (right == left) {
return;
}
int mid = left + (right - left) / 2;
mergeSort(array, left, mid);
mergeSort(array, mid + 1, right);
merge(array, left, right, mid);
}
private void merge(int[] array, int left, int right, int mid) {
int[] aux = new int[mid - left + 1];
for (int i = 0; i < mid - left + 1; i++) {
aux[i] = array[left + i];
}
int i = 0;
int j = mid + 1;
int k = left;
while (k <= right) {
if (j == right + 1) {
array[k++] = aux[i++];
} else if (i == mid - left + 1) {
array[k++] = array[j++];
} else {
if (array[j] < aux[i]) {
array[k++] = array[j++];
} else {
array[k++] = aux[i++];
}
}
}
}
/**
* quick sort. Given an array of integers, sort the elements in the array in
* ascending order. The quick sort algorithm should be used to solve this
* problem.
*/
public int[] quickSort(int[] array) {
if (array == null || array.length == 0) {
return array;
}
quickSort(array, 0, array.length - 1);
return array;
}
private void quickSort(int[] array, int left, int right) {
if (right <= left) {
return;
}
int pivot = partition(array, left, right);
quickSort(array, left, pivot - 1);
quickSort(array, pivot + 1, right);
}
private int partition(int[] array, int left, int right) {
int pivot = left + (int) (Math.random() * (right - left + 1));
swap(array, pivot, right--);
pivot = right;
int k = left;
while (k <= right) {
if (array[k] < array[pivot]) {
swap(array, left++, k++);
} else {
swap(array, k, right--);
}
}
swap(array, k, pivot);
return k;
}
/**
* Move 0s To The End I
* <p>
* Given an array of integers, move all the 0s to the right end of the array.
* The relative order of the elements in the original array does not need to be
* maintained.
*/
public int[] moveZero(int[] array) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
if (array[left] == 0) {
swap(array, left, right--);
} else {
left++;
}
}
return array;
}
/**
* Rainbow Sort
* <p>
* Given an array of balls, where the color of the balls can only be Red, Green
* or Blue, sort the balls such that all the Red balls are grouped on the left
* side, all the Green balls are grouped in the middle and all the Blue balls
* are grouped on the right side. (Red is denoted by -1, Green is denoted by 0,
* and Blue is denoted by 1).
*/
public int[] rainbowSort(int[] array) {
int i = 0;
int j = array.length - 1;
int k = 0;
while (k <= j) {
if (array[k] == -1) {
swap(array, i++, k++);
} else if (array[k] == 1) {
swap(array, k, j--);
} else {
k++;
}
}
return array;
}
private static void swap(int[] array, int a, int b) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}