3.3 Algorithms Flashcards

1
Q

What is a recursive algorithm?

A

An algorithm that calls upon 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 is a general and base case?

A

General case is a smaller instance of the same problem

Base case is what causes the algorithm to stop

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

Benefits and drawbacks to recursion

A

+ Natural way to process data
+ fewer lines of code, easier to read
- utilises stacks, which have limited size and stack overflows can occur
- takes longer to process due to stacking

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

Quicksort

A

divide and conquer sorting algorithm. very efficient and fast and often considered better than merge sort due to using less memory but this isn’t really the case as it depends on other factors

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

Quicksort Pseudocode

A

FUNCTION quick_sort(items, start, end)

IF start >= end THEN 
	RETURN

ELSE
	pivot_value = items[start]
	low_mark = start + 1
	high_end = end
   	finished = False

    WHILE finished = False
    	WHILE low_mark <= high_mark AND items[low_mark] <= pivot_value
            	low_mark = low_mark +1
            ENDWHILE

            WHILE low_mark <= high_mark AND items[high_mark] >= pivot_value
            	high_mark = high_mark - 1
	ENDWHILE

	IF low_mark < high_mark THEN
		temp = items[low_mark]
		items[low_mark] = items[high_mark]
                    items[high_mark] = temp

	ELSE
		finished = TRUE
	ENDIF
ENDWHILE

temp = items[start]
items[start] = items[high_mark]
items[high_mark] = temp

quick_sort(items, start, high_mark - 1)

    quick_sort(items, high_mark + 1, end)

ENDIF

RETURN items

ENDFUNCTION

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

Compare sorting algorithms and their time complexity

A

bubble sort - simple and easy to implement but only ideal for small data sets

best case (items in order) - O(n)
worst case (reverse order) - O(n^2)
average case - O(n^2)

Insertion sort - efficient for small or nearly sorted datasets

best case (items in order) - O(n)
worst case (reverse order) - O(n^2)

merge sort- very fast and efficient
but uses additional memory for merging
best case/average case/worst case is O(n log n) as always splits array and merges regardless of list size

quick sort - fast efficient and ideal for large data sets but can use memory (varies on factors like pivot placement)

best case (list is partioned into two equal sizes) - O(n log n)
worst case (unbalanced partion) - O(n^2)
average case - varies on factors but generally is O(n log n)

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

space complexities of algorithm

A

Bubble sort/Insertion sort - O(1) as sorting is done in same space as original data

Quick sort - sorting done in same space however since its a recursive algorithm it has stack processing which at its most efficieint makes it O(log n)

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

Algorithm

A

An unambigous set of instructions to solve a problem. Example being flowchart or psuedocode

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

Iteration

A

Repeating a set of instructions until a condtion is met

+easier to look at and debug
+ generally uses less memory than recursive too.

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