-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayTest2.java
More file actions
199 lines (163 loc) · 5.32 KB
/
Copy pathArrayTest2.java
File metadata and controls
199 lines (163 loc) · 5.32 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
package array;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;
public class ArrayTest2 {
/* question 1:
create an array with length of 6, pick random value from 1 to 30, no repeated numbers are allowed
*/
// int random2 = (int)Math.random() * (99 - 10 + 1) + 10;
@Test
public void test1(){
int[] arr = new int[6];
// generate random value from 1 - 30
int min = 1;
int max = 30;
Random random = new Random();
int[] rans = new int[30];
for (int i = 0; i < rans.length; i++) {
int ran = random.nextInt(max - min + 1) + min;
rans[i] = ran;
System.out.print(rans[i] + " ");
}
// for (int i = 0; i < arr.length; i++) {
// arr[i] = ran;
// }
}
@Test
public void maxMinSumTest2(){
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
int random = (int)(Math.random() * (99 - 10 + 1) + 10);
arr[i] = random;
System.out.print(arr[i] + " ");
}
System.out.println();
// get max value
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
System.out.println("max value: " + max);
// get min value
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] < min){
min = arr[i];
}
}
System.out.println("min value: " + min);
// get sum
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("total sum: " + sum);
}
// copy arr1 from arr
/*
* int[] arr = new int[]{1,2,3,...}
* int[] arr1 = new int[arr.length]
*
* for (int i = 0; i < arr.length; i++) {
arr1[i] = arr[i];
}
* */
// this is not copy, they still point to the same arr
// arr1 = arr;
/* reverse the array */
@Test
public void reverseTest(){
String[] names = new String[]{"AA", "BB", "CC"};
// method 1
/* for (int i = 0; i < names.length/2; i++) {
String temp = names[i];
names[i] = names[names.length -1 -i];
names[names.length -1 -i] = temp;
}*/
// method 2
for (int i = 0, j = names.length - 1; i < j; i++, j--) {
String temp = names[i];
names[i] = names[j];
names[j] = temp;
}
System.out.println(Arrays.toString(names));
// method 3 (the worst)
/* String[] names = new String[]{"AA", "BB", "CC"};
String[] names2 = new String[names.length];;
for (int i = 0; i < names.length; i++) {
// reverse
names2[i] = names[names.length -1 -i];
}
for (int i = 0; i < names2.length; i++) {
System.out.println(names2[i]);
}*/
}
@Test
public void searchTest(){
String[] names = new String[]{"AA", "BB", "CC"};
Boolean isFlag = false;
String dest = "BB";
dest = "CC";
// method 1: linear search
/*for (int i = 0; i < names.length; i++) {
if(dest.equals(names[i])){
System.out.println("we found BB in names, position is : " + i);
isflag = true;
}
}
if(!isflag){
System.out.println("sorry, no result");
}*/
// method2: binary search (array should be sorted in order (doesn't matter desc or asc), much more effiecit than the linear)
/* int[] arr = new int[]{-90, -19, -9, 0, 20, 45,74,100, 333};
int head = 0;
int end = arr.length -1;
int dest1 = 333;
dest1 = 10;
Boolean isFlag1 = true;
while(head <= end){
int middle = (int)(head + end) / 2;
if (dest1 == arr[middle]){
System.out.println("the dest is at position: " + middle);
isFlag1 = false;
break;
}else if(dest1 > arr[middle]){
head = middle + 1;
}else if(dest1 < arr[middle]){
end = middle -1;
}
}
if(isFlag1){
System.out.println("sorry, no result found");
}*/
int[] arr2 = new int[]{-90, -19, -9, 0, 20, 45,74,100, 333};
int dest2 = 21;
int index = Arrays.binarySearch(arr2, dest2);
if(index >= 0){
System.out.println("the dest is at position: " + index);
}else{
System.out.println("sorry, no result found");
}
}
@Test
public void sortTest(){
int[] arr = new int[]{-90, 19, -9, 40, 20,74, -87, 100, 333, 250};
// method 1 bubble sorting
/* for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length -1 - i; j++) {
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}*/
// method 2 : quick sorting (better, more efficient)
// .......
Arrays.sort(arr); // source code of sort() is using quick sort actually.
System.out.println(Arrays.toString(arr));
}
}