CompSci - Algorithms Flashcards
whjat
Recursion
Occurs when a function calls itself (not subroutine; that has no return value)
Stack Overflow
While recursion occurs, memory is filled with return addresses and local variables. If these exceed allocated memory capacity, stack overflow occurs, causing the program or device to crash.
For this reason, stopping conditions must be included in recursive algorithms.
Stopping Condition
A condition in a recursive algorithm that stops it once some criteria has been reached.
Validation
Automatic proccess that is done by an algorithm to ensure that data input by the user is appropriate and follows set rules.
Length, Lookup, Range, Presence, Type, Format
Verification
Manual process that is done by the user themselves to check that data input is correct.
Double-Keying, Proofreading
Authentication
Automatic process done by an algorithm that compares data input by the user with appropriate previously entered data to check that data is correct.
2FA, Biometrics, Password
Trace Tables
IDE tool that helps to keep track of variables’ values as an algorithm runs. Useful for debugging by comparing with expected values.
Dry-Running
Manually working through an algorithm without the use of a computer.
Dry-Running Benefits
Uses pseudocode, which is more easy to understand and write
Helps the programmer to become familliar with the process
Low stakes
Search Algorithms
Linear
Binary
Binary Tree
Shortest Path
Sort
Bubble
Insertion
Merge
Quicksort
Quicksort Algorithm
quicksort(data, first, last)
if data length > 1:
if first < last:
pivot = data[first]
up = 1
down = last
while up < down:
while up <= down and data[up] <= pivot:
up += 1
while up <= down and data[down] >= pivot:
down -= 1
if up < down:
temp = data[up]
data[up] = data[down]
data[down] = temp
temp = data[first]
data[first] = data[down]
data[down] = temp
quicksort(data, first, down-1) quicksort(data, down+1, last)
Algorithm Definition Methods
Flowcharts, pseudocode
Benefits & Drawbacks
ACCESS MDC
Accessibility
Cost
Convenience
Efficiency
Security
Scalability
Maintenance
Depencence
Complexity
Bubble Sort
Repeatedly compares two consecutive elements in a list, swapping them if they are in the wrong order.
Requires multiple passes.
Insertion Sort
Iterate over elements starting from the second.
For each element, while the current is < previous and previous exists, replace the current index’ element with the previous index’ element and select the index previous to the previous index.
Else replace the next index’ element to the index previous to the previous index with the current element.
Quicksort
Select the first value of the list as the pivot.
Select the second and last values as the low and high marks respectively.
While the low mark element is lower than the pivot, move the low mark up the list.
While the high mark element is higher than the pivot, move the high mark down the list.
If low mark is lower than high mark, swap the elements of the two.
If high mark becomes lower than low mark, swap the pivot value and high mark element.
Repeat this process for each partition that the pivot value seperates until the partition only has one element in it.
Recursive.
Merge Sort
Select the middle element of the list and separate it into two sublists with the middle element part of the left sublist. Keep separating the sublists until they all contain one element each.
Compare elements left-to-right when merging sublists until an ordered list is formed.