Algorithms Flashcards
1
Q
What is sequential search?
A
Searches each element sequentially
2
Q
What is binary search?
A
Examines the middle of each array, if it’s too big, eliminate the right have of the array and repeat
3
Q
What is the runtime of sequential search?
A
N
4
Q
What is the runtime of binary search?
A
Eliminates 1/2 of the array until 1 element remains, so how many divisions does it take?
Log(base 2) N
5
Q
Write a program that prints the nth Fibonacci number
A
class fibonacci { static int fib(int n) { int a = 0, b = 1, c; if (n == 0) return a; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; }
public static void main (String args[]) { int n = 9; System.out.println(fib(n)); } }