CompSci - Algorithms Flashcards
whjat (27 cards)
Recursion
Occurs when a function calls itself (not subroutine; 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
Sort Algorithms
Bubble
Insertion
Merge
Quicksort
Variety in recursion needed for more memory control and speed
Algorithm Definition Methods
Flowcharts, pseudocode
General 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.
Pseudocode
Two flags
Reset swap at start of each pass, if no swaps all pass, sorted
Insertion Sort
Separates the unsorted array into a “sorted” left side and “unsorted” right side. It iteratively compares values left to right and orders them appropriately.
Pseudocode
Starts from second element
Define current element and previous index
While current < prev & prev exists
Replace current with prev
Define previous previous index
OUTSIDE FOR replace prev with current
[i] only used in initial for, then in terms of [prev]
Quicksort
Select an arbitrary value as a pivot, then separate the list into two sublists of elements lesser and greater than the pivot. These sublists are then sorted and ordered correctly.
Recursive.
Pseudocode
a,start,end parameters, if start>=end else,def pivot, low,high,sorted while while obv go closer, if obv swap low high. outside swap start high. quicksort start high -1 and high+1 start
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.
Recursive
Pseudocode
Two functions, merge & sort
merge: def i_l/r=0 and merged, compare from left to right and append if true. return merged
sort: if len <= 1, def mid,l,r, sort each and merge both. return merged
Algorithm Definition
A defined set of sequential operations to apply to data in order to achieve a useful output.
Sorting Algorithm Efficiency Factors
Extra memory needed in addition to the input data
How many comparisons the CPU makes between data
How often two elements are swapped
How many times the algorithm scans the entire dataset
Data Compression
Representing the same information using fewer bits for more efficiency
Data Compression Algorithms
Run-Length Encoding - Replace repeated values with an instance and a count. Lossless
Huffman Encoding - Binary tree created based on character frequency, where traversal is optimised for most frequent characters. Lossless
Truncation - Redundant data is removed. Lossy
Dictionary Encoding - Dictionary created, containing frequently repeating patterns and their keys. Lossless
Lossy and Lossless Algorithms
Lossy - JPEG, MP3
Lossless - PNG, ZIP
Shortest Path Algorithms
Djikstra’s Algorithm - Find smallest weighing of adjacent nodes from the start until the end node is reached.
A* Algorithm - Estimates the smallest weighing heuristically until the shortest path is found.
Binary Search
Compare searched value with midpoint of sorted list. If greater of smaller than it, compare it with the midpoint of the respective sublist. Repeat until a midpoint is equal or not found.
Pseudocode
Def left,right, while left<right compare value with array[mid]