Test Flashcards

1
Q

What is the difference between an iterative algorithm and a recursive algorithm?

A

Iterative - uses loops to repeat a set of instructions until a condition is met

Recursive - solves a problem by calling itself with a smaller or simpler version of the original problem

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

What is a recursive algorithm’s base case? What is the recursive case?

A

Base - the simplest, smallest instance of the problem that can be solved directly without further recursion.

Recursion - the part of the algorithm that breaks the problem into one or more smaller subproblems and calls itself to solve these subproblems

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

What type of recursive method do you think would be more difficult to debug: one that uses direct recursion or one that uses indirect recursion? Why?

A

Indirect recursion is generally more difficult to debug due to the added complexity in following the call sequence across multiple functions.

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

Which repetition approach is less efficient: a loop or a recursive method? Why?

A

Generally, recursion is less efficient than iteration (loops) because of the additional overhead associated with multiple function calls.

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

When recursion is used to solve a problem, why must the recursive method call itself to solve a smaller version of the original problem?

A

Solving a smaller version of the problem at each step reduces its complexity until the base case is reached

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

How is a problem usually reduced with a recursive method?

A

Split the original problem, solve the smaller instances using a recursive call, and then combine the results

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

If a sequential search method is searching for a value that is stored in the last element of a 10,000-element array, how many elements will the search code have to examine to locate the value?

A

It will examine all 10,000 elements

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

In an average case involving an array of n elements, how many array elements does a sequential search algorithm have to access to locate a specific value?

A

In the average case, a sequential search examines about n/2 elements.

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

A binary search method is searching for a value that is stored in the middle element of an array. How many array elements have to be examined before finding the value?

A

It will examine 1 element—the middle element—since the binary search checks the midpoint first.

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

What is the maximum number of comparisons that a binary search method will make when searching for a value in a 1,000-element array?

A

Approximately 10 comparisons.

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

Why is the bubble sort inefficient for large arrays?

A

Because its O(n²) time complexity leads to a very high number of comparisons and swaps, making it inefficient for large arrays.

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

Under what conditions might the insertion sort be more efficient than the bubble sort?

A

When the array is nearly sorted, because insertion sort requires fewer shifts and comparisons in such cases, making it more efficient than bubble sort.

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