Algorithms and programming Flashcards

1
Q

What is a recursive algorithm?

A

A recursive algorithm is one that repeatably calls itself until a base case is met.

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

What are the advantages of recursion?

A

More natural to read.
Suited to certain problems, for example those using trees.
Quicker to write as some functions are naturally recursive.
Can reduce the size of the problem with each call

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

What are the disadvantages of recursion?

A

Can run out of stack space / memory (due to too many calls) causing it to crash. This can be avoided with tail recursion.
More difficult to trace / follow as each frame on the stack has its own set of variables
Requires more memory than the equivalent iterative algorithm.
Usually slower than iterative methods due to maintenance of the stack

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

What is iteration?

A

Iteration is when the same procedure is repeated using a count controlled ‘for’ loop or a condition controlled ‘while’ loop.

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

Identify where the recursion is taking place.

01 def factorial(n)
02      if n == 0:
03          return 1
04     else:
05         return n * factorial(n-1)
A

Line 05

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

Identify the base case/terminating condition.

01 def factorial(n)
02      if n == 0:
03          return 1
04     else:
05         return n * factorial(n-1)
A

Line 02

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

What is the worst case number of searches for binary search?

A

O(log2(n)) when the search reaches the deepest level of the tree.

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

What is the best case number of searches for binary search?

A

O(1) when the target value is the middle element.

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

What is the average case number of searches for binary search?

A

O(log2(n) -1)

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

What is the worst case number of searches for linear search?

A

O(n) when the value at the end of the list or not in the list so that n comparisons are needed.

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

What is the best case number of searches for linear search?

A

O(1) when the value is equal to the first element of the list so only one comparison is needed

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

What is the average case number of searches for linear search?

A

O(n/2)

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

What is logarithmic complexity O(log n)?

A

The inverse of exponential growth. (Halving a dataset)

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

What is linear complexity O(n)?

A

An algorithm who’s complexity will grow in direct proportion to the size of the input data. (For/While loop)

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

What is constant complexity O(1)?

A

Algorithm that will always execute in the same time regardless of the size of the input data set. (No loops)

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

What is polynomial complexity O(n^2)?

A

An algorithm whose performance is directly proportional to the squared size of the input data set. (Nested loops)

17
Q

What is exponential complexity O(2^n)?

A

An algorithm whose growth doubles with each addition to the input data set (Recursion)

18
Q

Write a binary search algorithm in pseudocode.

A

def binary

19
Q

In what situation would you use a linear search?

A

Unsorted array as it does not need to be sorted, useful if often inserting/removing
Array is very small
Item being searched for is very close to start of the array

20
Q

In what situation would you use a binary search?

A

Sorted array

Large amount of data

21
Q

What are the key steps of quicksort

A

Quicksort is a divide and conquer algorithm

1) Pick an element in the array as the “pivot”
2) In the partition phase it “partitions” the array into two divisions: Items to the left of the pivot are less than the “pivot”, and items to the right of the pivot are greater than the “pivot”
3) It does this recursively on smaller and smaller subarrays until the entire array is sorted.

22
Q

What is the worst case complexity of quicksort?

A

O(n^2)

23
Q

What is the best and average case complexity of quicksort?

A

O(n log n)

24
Q

What are the advantages of quicksort?

A

On average it runs very fast.

It requires no additional memory.

25
Q

What are the disadvantages of quicksort?

A

Quicksort’s running time degrades if given an array that is almost sorted (or almost reverse sorted).

26
Q

Write pseudocode for a bubble sort

A
SET counter = 0
SET swapped = True
SET swaps = 0
SET length TO LENGTH(list)
WHILE swapped == True DO
      WHILE counter < length – 1 DO
           IF list[counter] > list[counter + 1] THEN
                 SET temp TO list[counter]
                 SET list[counter] TO list[counter + 1]
                 SET list[counter + 1] TO temp
                 SET swaps TO swaps + 1
           END IF
           SET counter TO counter + 1
           SET length TO length - 1
       END WHILE
       IF swaps = 0 THEN
            SET swapped TO False
       ELSE
             SET swaps TO 0
             SET counter TO 0
       END IF
END WHILE
27
Q

What are the advantages of bubble sort

A

Simple to write and understand