solution:
boolean contains (int array[], int key) {
for (int i=0; i<array.length; i++)
if (array[i] == key) return true;
return false;
}
solution:
boolean contains (int array[], int key) {
int low = 0;
int high = list.length - 1;
while (low <= high) {
int mid = (low + high)/2;
if (list[mid] == value)
return true;
else if (list[mid] < value)
low = mid + 1;
else
high = mid - 1;
}
return false;
}
solution:
boolean contains (int array[][], int key) {
for (int i=0; i<array.length; i++)
for (int j=0; j<array[i].length; j++)
if (array[i][j] == key) return true;
return false;
}
solution:
int v[][] = new int[50][100];
Random rand = new Random ();
int sum = 0;
for (int i=0; i<50; i++) {
for (int j=0; j<100; j++) {
v[i][j] = rand.nextInt(100) + 1;
sum += v[i][j];
}
}
System.out.println (sum / 5000.0);
solution
O(n)
solution
O(log n)
solution
O(n)
solution
O(n2)
solution:
double findMin (double v[]) {
double min = v[0];
for (int i=1; i<v.length; i++) {
if (v[i] < min) min = v[i];
}
return min;
}
class foo {
static void m1 (String v[], int i, String s) {
v[i] = s;
}
static void m2 (String v[], int i, String s) {
String w[] = new String[v.length];
for (int j=0; j<w.length; j++) w[j] = v[j];
v = w;
v[i] = s;
}
static void m3 (String s, String t) {
s = t;
}
public static void main (String args[]) {
String x[] = new String[3];
x[0] = "a";
x[1] = "b";
x[2] = "c";
m1 (x, 2, "d");
System.out.println (x[2]);
m2 (x, 2, "e");
System.out.println (x[2]);
m3 (x[2], "f");
System.out.println (x[2]);
}
};
Answer the following questions:
solution: Yes. The parameter v in m1 is a reference to the array x, so the #2 element of x is change from "c" to "d".
solution: No. v is a reference to x. m2 makes a copy of the array x, assigns a reference to the copy to v, and then changes the #i element of v. However, x remains unchanged because v is only the value of the reference to x; changing v does not change x.
solution: No. The reference s is change to the value of the reference t, but the original reference x[2] remains unchanged.