Binary

Practice Questions

Computer Science › Binary

Questions
3
1

public static int foo(int\[\] arr, int x) {

int a = 0;

int b = arr.length - 1;

while(b >= a) {

int c = (a + b) / 2;

int v = arr\[c\];

if(v == x) {

return c;

} else if(v < x) {

a = c + 1;

} else {

b = c - 1;

}

}

return -1;

}

What is the value of y in the code below:

int\[\] vals = {1,3,4,5,6,31,41,51};

int x = 41;

int y = foo(vals,41);

2

What is the difference between inorder traversal of a binary search tree and a preorder traversal?

3

Which of the following is true for a binary search algorithm?

Return to subject