Recursion Flashcards

1
Q

Unary v Binary v n-ary Recursion

A

Unary - single recursive call is used
Binary - two recursive calls are used (so maybe go left & right)
n-ary - n recursive calls

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

Direct v Indirect recursion

A

Direct - recursive calls go to the same function
Indirect - recursion goes through two or more methods (p calls q, q calls p etc)

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

Tail recursion

A

Where the result of the recursive call is the result of the function
(so nothing is done on the way back)

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

Requirements for Recursion

A
  1. base case/s
  2. recursive call, where the result is combined
  3. convergance to the base case (so it becomes more simple each time)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Key Idea of Merge Sort

A
  1. Split the array into two halves
  2. Sort the smaller arrays
  3. Merge two sorted halves into a single sorted array

Trival split, complex merge

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

Merge Sort Psuedocode

A

Args: array, start, end, tmp array

if start != end:
- mid -> (start+end)//2
- recursive call (array, start, mid, tmp)
- recursive call (array, mid+1, end, tmp)
- call merge arrays (array, start, mid, end, tmp)

  • copy array into temp from start to end - range(start, end+1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Merge Arrays (Merge Sort) Psudeocode

A

ia -> start index
ib -> mid index + 1

for i in range(start, end+1):
- if ia > mid: (meaning half1 is finished, use half2)
set tmp[k] to a[ib]
ib += 1
- elif ib > end: (meaning half 2 is finished, use half1)
set tmp[k] to a[ia]
ia += 1
- elif a[ia] <= a[ib]:
tmp[k] = a[ia]
ia += 1
- else
tmp[k] = a[ib]
ib += 1

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

Quick Sort Psuedocode

A

Args: array, start, end

if start < end:
- boundary = partition(array, start, end)
- recursive_call(array, start, boundary-1)
- recursive_call(array, boundary+1, end)

complex split, no merge required

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

Partition (quick sort) psuedocode

A

Args: array, start, end

mid -> (start+end)//2
pivot -> array[mid]
swap(array, start, mid)

boundary -> start

for k in range(start+1, end+1):
- if array[k] < pivot:
- boundary += 1
- swap(array, k, boundary)

swap(array, start, boundary)
return boundary

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

Partition (quick sort) key idea

A

Pick a pivot (ideally the median)
move elements less than the pivot to the left, and greater than the pivot to the right

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

Quick Sort complexity

A

Best Case: O(n log(n))Comp - when pivot is median
Worst Case: O(n^2)
compeq - when pivot is max/min

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

Merge Sort complexity

A

Best/Worst Case: O(n log(n))*Comp

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

Merge Sort v Quick Sort v other sorts

A

Merge Sort:
- better complexity, lower number of comparisons
- however, requires a temp array, so way more space is needed

Quick Sort:
- worse complexity
- but no need to copy back, so a lower constant if the pivot is good

Other sorts:
- both of these require recursion, so overhead is higher, and may not be worth it for smaller arrays

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