Algorithms Flashcards

1
Q

What is sequential search?

A

Searches each element sequentially

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the runtime of sequential search?

A

N

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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));
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly