for i from n − 1 downto 1 do
j ← random integer such that 0 ≤ j ≤ i
exchange a[j] and a[i]
ANSWER:
public static void shuffle(int[] data) {
Random ran = new Random();
int n = data.length;
for (int i = data.length-1; i >= 1; --i) {
int j = rgen.nextInt(i+1); // random int between 0 and i inclusive
// swap those elements in data
int temp = data[j];
data[j] = data[i];
data[i] = temp;
}
}
public class M
{
public static int f(int[] arr, int i, int j)
{
int m;
int b, c;
if (i == j) {
return arr[i];
}
else {
m = (i + j) / 2;
b = f(arr, i, m);
c = f(arr, m + 1, j);
if (b < c) {
return b;
}
else {
return c;
}
}
}
public static void main(String[] args)
{
int a;
int[] A = {1,6,3,9,10,11,5};
a = f(A, 0, A.length - 1);
System.out.printf("a = %d\", a);
}
}
ANSWER: The minimum value in the array.
ANSWER:
public E peek() {
if (isEmpty())
return null;
else
return head.next.data; // data is the name of the item field
}
ANSWER:
public Deque<E> listUnion(Deque<E> lst2) {
// (0) create the resulting queue
Deque<E> lst3 = new Deque<E>();
// (1) insert all (unique) elements from this list in lst3
Iterator it1 = this.iterator();
while (it.hasNext())
lst3.addLast(it.next());
// (2) insert other elements from lst2 in lst3
Iterator it2 = lst2.iterator();
boolean found;
while (it2.hasNext()) {
E val2 = it2.next();
found = false;
it1 = lst1.iterator(); // iterator to this list
boolean found = false;
while (it1.hasNext()) {
E val1 = it1.next();
if (val1.equals(val2)) { // same element exists in this list
found = true;
break;
}
}
if (!found)
lst3.addLast(val2);
} // end outer-while
return lst3;
}
ANSWER: a -- O(1), b --
O(n^2)
ANSWER: b (64 <== 8^2)
ANSWER: c (24 <==
8*lg(8) == 8*3))
int sum = 0;
for (int i = 1; i <= n; i++) {
int j = i;
while (j > 0) {
sum += j;
j /= 2;
}
}
ANSWER: O(n log n) int sum = 0; int i, j; for (i = 1; i <= n/2; i++) sum++; for (j = i; j <= n; j++) sum++;ANSWER: O(n)
int sum = 0; int i = 1; j = n * n; while (i++ < j--) sum++;ANSWER: O(n^2)
ANSWER:
// version 1: O(n^2)
public boolean areListsEqual1(ArrayList<Integer> ar1, ArrayList<Integer> ar2) {
int len = ar1.size();
if (len != ar2.size())
return false;
else {
for (int i = 0; i < len; ++i) {
Integer val1 = ar1.get(i);
boolean found = false;
for (int j = 0; j < len; ++j) {
if (ar2.get(j).equals(val1)) {
found = true;
break;
}
}
// if flag has remained false, no matching element was found for val1
if (!found)
return false; // early exit
}
// all elements in ar1 had a counterpart in ar2
return true;
}
}
// version 2: O(n log n)
public boolean areListsEqual2(ArrayList<Integer> ar1, ArrayList<Integer> ar2) {
int len = ar1.size();
if (len != ar2.size())
return false;
else {
// sort both arrays first
Collections.sort(ar1);
Collections.sort(ar2);
// traverse two arrays simultaneously
for (int i = 0; i < len; ++i) {
if (!ar1.get(i).equals(ar2.get(i))
return false; // early exit
}
// all elements matched
return true;
}
}
void Bubblesort(int[] data)
{
int tmp,i,j;
int n = data.length;
for (i=0; i<n-1; i++) {
for (j=0; j<n-i-1; j++)
if (data[j] > data[j+1]) {
tmp = data[j];
data[j] = data[j+1];
data[j+1] = tmp;
}
}
}
ANSWER:
+---+---+---+---+
initially | 5 | 3 | 2 | 4 |
+---+---+---+---+
+---+---+---+---+
i=0, j=0 | 3 | 5 | 2 | 4 |
+---+---+---+---+
+---+---+---+---+
i=0, j=1 | 3 | 2 | 5 | 4 |
+---+---+---+---+
+---+---+---+---+
i=0, j=2 | 3 | 2 | 4 | 5 |
+---+---+---+---+
+---+---+---+---+
i=1, j=0 | 2 | 3 | 4 | 5 |
+---+---+---+---+
+---+---+---+---+
i=1, j=1 | 2 | 3 | 4 | 5 |
+---+---+---+---+
+---+---+---+---+
i=2, j=0 | 2 | 3 | 4 | 5 |
+---+---+---+---+
ANSWER:
import java.util.Arrays;
import java.util.Comparator;
class IgnoreCaseComp implements Comparator<String>
{
public int compare(String s1, String s2)
{
return s1.toLowerCase().compareTo(s2.toLowerCase()); // or upper case
}
}
public class ICCDemo {
public static void main(String[] args) {
String[] sar1 = {"abc", "ABC"};
String[] sar2 = Arrays.copyOf(sar1, sar1.length);
Arrays.sort(sar1);
Arrays.sort(sar2, new IgnoreCaseComp());
System.out.println("sar1 (with compareTo()): " + Arrays.toString(sar1));
System.out.println("sar2 (with ignore case): " + Arrays.toString(sar2));
}
}
/* Output of the program
sar1 (with compareTo()): [ABC, abc]
sar2 (with ignore case): [abc, ABC]
*/

ANSWER:
(1) First place 1 at the end of the queue
0 1 2 3 4 5 6 7 8 9 10
5 +----+----+----+----+----+----+----+----+----+----+----+
/ \ | 5 | 8 | 50 | 11 | 10 | 52 | 55 | 25 | 22 | 20 | 1 |
8 50 +----+----+----+----+----+----+----+----+----+----+----+
/ \ / \
11 10 52 55
/ \ / \
25 22 20 1
(2) Swap 1 and 10 (because 10 > 1)
0 1 2 3 4 5 6 7 8 9 10
5 +----+----+----+----+----+----+----+----+----+----+----+
/ \ | 5 | 8 | 50 | 11 | 1 | 52 | 55 | 25 | 22 | 20 | 10 |
8 50 +----+----+----+----+----+----+----+----+----+----+----+
/ \ / \
11 1 52 55
/ \ / \
25 22 20 10
(3) Swap 1 and 8 (because 8 > 1)
0 1 2 3 4 5 6 7 8 9 10
5 +----+----+----+----+----+----+----+----+----+----+----+
/ \ | 5 | 1 | 50 | 11 | 8 | 52 | 55 | 25 | 22 | 20 | 10 |
1 50 +----+----+----+----+----+----+----+----+----+----+----+
/ \ / \
11 8 52 55
/ \ / \
25 22 20 10
(4) Swap 1 and 5 (because 5 > 1) --the final tree/queue
0 1 2 3 4 5 6 7 8 9 10
1 +----+----+----+----+----+----+----+----+----+----+----+
/ \ | 1 | 5 | 50 | 11 | 8 | 52 | 55 | 25 | 22 | 20 | 10 |
5 50 +----+----+----+----+----+----+----+----+----+----+----+
/ \ / \
11 8 52 55
/ \ / \
25 22 20 10
ANSWER:
(1) Pop the root (4) and replace it with the last element in the queue (50).
0 1 2 3 4 5 6 7 8
50 +----+----+----+----+----+----+----+----+----+
/ \ | 50 | 11 | 17 | 22 | 19 | 20 | 60 | 65 | 30 |
11 17 +----+----+----+----+----+----+----+----+----+
/ \ / \
22 19 20 60
/ \
65 30
(2) Swap 50 and 11 (because 11 < 17 and 50 > 11)
0 1 2 3 4 5 6 7 8
11 +----+----+----+----+----+----+----+----+----+
/ \ | 11 | 50 | 17 | 22 | 19 | 20 | 60 | 65 | 30 |
50 17 +----+----+----+----+----+----+----+----+----+
/ \ / \
22 19 20 60
/ \
65 30
(3) Swap 50 and 19 (because 19 < 20 and 50 > 19) --the final tree/queue
0 1 2 3 4 5 6 7 8
11 +----+----+----+----+----+----+----+----+----+
/ \ | 11 | 19 | 17 | 22 | 50 | 20 | 60 | 65 | 30 |
19 17 +----+----+----+----+----+----+----+----+----+
/ \ / \
22 50 20 60
/ \
65 30
ANSWER: parent of the last leaf node, found at index (n - 1 - 1) / 2 = (n - 2) / 2
ANSWER:
Initial: [3, 1, 8, 5, 4, 7, 6] After max-heap: [8, 5, 7, 1, 4, 3, 6] After 1st iter: [7, 5, 6, 1, 4, 3, 8] After 2nd iter: [6, 5, 3, 1, 4, 7, 8]
ANSWER:
| Initial |
0 1 2 3 4 5 6 7 8 9
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(9,0) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(3,4) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 5 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(5,8) | +---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 8 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(7,2) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 8 | 6 | 2 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(2,1) |
0 1 2 3 4 5 6 7 8 9
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 1 | 4 | 4 | 8 | 6 | 1 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(5,7) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 1 | 4 | 4 | 1 | 6 | 1 | 1 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(0,3) |
+---+---+---+---+---+---+---+---+---+---+ id | 4 | 1 | 1 | 4 | 4 | 1 | 6 | 1 | 1 | 4 | +---+---+---+---+---+---+---+---+---+---+ |
| union(4,2) |
+---+---+---+---+---+---+---+---+---+---+
id | 1 | 1 | 1 | 1 | 1 | 1 | 6 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+---+---+
|
ANSWER:
| Initial |
0 1 2 3 4 5 6 7 8 9
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(9,0) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(3,4) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 5 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(5,8) | +---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 8 | 6 | 7 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(7,2) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 2 | 4 | 4 | 8 | 6 | 2 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(2,1) |
0 1 2 3 4 5 6 7 8 9
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 1 | 4 | 4 | 8 | 6 | 2 | 8 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(5,7) |
+---+---+---+---+---+---+---+---+---+---+
id | 0 | 1 | 1 | 4 | 4 | 8 | 6 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(0,3) |
+---+---+---+---+---+---+---+---+---+---+
id | 4 | 1 | 1 | 4 | 4 | 8 | 6 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
| union(4,2) |
+---+---+---+---+---+---+---+---+---+---+
id | 4 | 1 | 1 | 4 | 1 | 8 | 6 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+---+---+
|
1 6
/ | \
2 4 8
| / \ |
7 0 3 5
|
9