Test 1 Flashcards
What are we considering when we talk about efficiency?
The amount of time and storage required to execute the algorithm
How do we measure efficiency?
With the help of asymptotic notations
What does O signify?
The maximum running time an algorithm can have, providing an estimation of its worst-case complexity
Why is O-Notation helpful?
It is a measure of run time that
- is independent of a specific machine or implementation
- shows us how the run time scales with the size of the input n
- is algebraically as simple as possible
What are the key steps for identifying O-notation?
- identify the size of the input (like length of list being sorted, number of entries in a data structure)
- identify one particular operation, called a basic operation, that is fundamental to the algorithm (comparison of elements in a list, follow a pointer in a traversal)
- count the number of basic operations that the algorithm performs as a function of n.
What is the order of common run times?
O(1)<=O(logn)<=O(n)<=O(nlogn)<=O(n^k)<=O(k^n)
What is recursion?
The process in which a function calls itself directly or indirectly. A recursive function solves a particular problem by colling a copy of itself and solving smaller subproblems (“smaller” means closer to the base case)
How do proofs by induction work?
To prove some statement P(n) is true for all n >=1, prove P(1) then assume P(n-1)
What are the main parts of a recursive method?
Base case(s): one or more parameter values for which the function does not call itself. The answer is computed directly
Recursive case: the answer to the current problem is calculated from on one or more answers to smaller versions of the problem
What happens when a function is called?
An activation record is created on the run-time stack. This record contains:
- the parameters received by the function
- the local variables
- the return address to the location in the original routine where this function was called
When the function returns, it is removed from the stack.
How do you calculate run time for recursive functions?
In this course, if the function carries out a constant number of operations other than the recursive call, then you can estimate the run time by counting the function calls.
What is the height of a binary tree?
O(log(n))
What are advantages/disadvantages of recursion?
Advantages:
- clean and simple
- MIGHT improve the time complexity
Disadvantages:
- generally requires more space
- generally takes more time to run
- repeated function calls introduce more overhead
- can unnecessarily duplicate work (fibonacci, exponent examples)
When should we use recursion?
- if the recursive method performs redundant calculations, switch to iterative
- if the recursive method is no simpler than the iterative method, switch to iterative
- only use recursion on naturally recursive problems, where the recursive solution is the easiest to implement and understand
What are some applications of recursion?
- tree and graph traversal
- sorting algorithms
- divide-and-conquer algorithms
- backtracking algorithms
What is a divide and conquer algorithm?
A strategy for solving a large problem by breaking the problem into smaller sub-problems
1. dividing the problem into smaller, more manageable sub-problems
2. solving these sub-problems individually
3. combining their solutions to obtain the desired overall result
What are some properties to consider when comparing different algorithms?
- correctness
- time required
- memory space required
- simplicity/clarity/maintainability
What are some factors that affect the amount of time it takes to run a program?
- the conditions of the computer
- the choice of programming language, complier,
- the quality and accuracy of the implementation
- the size of the input
- whether the data is convenient (like key for a search being the first thing considered)
What is the process of the insertion sort?
Divides the array into two parts: a sorted section and an unsorted section. Elements from the unsorted part are selected and placed in the correct order within the sorted section
What is the time and space complexity of the insertion sort?
Iterative best case:
Time: O(n)
Spacce: O(1)
Iterative worst case:
Time: O(n^2)
Space: O(1)
Recursive:
Time: O(n^2)
Space: O(n) (stack frames!!)
How do you do an insertion sort using recursion?
Base case: if the array size is 1 or smaller, return
Recursively sort first n-1 elements
Insert the last element at its correct position in the sorted array
What are advantages and disadvantages of insertion sort?
Advantages:
- simple implementation
- efficient for small data values
- appropriate for data sets that are already partially sorted
Disadvantages:
- with a time complexity of O(n^2), it is not efficient for big data sets
What is the process of the selection sort?
- select the smallest element from the unsorted portion of the list
- move it to the sorted portion of the list
- repeat until the sorted portion is empty
What is the time and space complexity of selection sort?
Best case and worst case:
Time: O(n^2)
Space: O(1)
What is the process for the merge sort?
- Divide: split the unsorted array into two equal halves until you reach arrays with one element
- Conquer: start from the bottom of the tree and combine (merge) each pair of unsorted arrays