Data structures and algorithms Flashcards
Characteristics of an Abstract Data Type (ADT)
Specifies an interface, not an implementation. While it must respond to method calls, implementation is not important.
Bag ADT
- add
- remove
- contains
- numItems
- grab
- toArray
Formula for comparisons in selection sort
O(n^2)
This will be the efficiency regardless of input
Formula for moves in selection sort
O(n)
This will be the efficiency regardless of input
Radix Sort
-Sort by properties rather than values.
Example: sort by the individual digits in a three-digit number instead of the value of the number.
- properties move from right to left (i.e. one’s place first)
- original array is processed from left to right
Bubble Sort Performance
O(n^2)
note: even worse performance than other O(n^2) algorithms.
- not adaptive (won’t be any faster, even on fully sorted input)
- high number of moves and comparisons (other O(n^2) algorithms only have a high number of moves or comparisions
Algorithm performance classes
O(1) constant time O(log n) logarithmic time O(n) linear time O(n log n) n log n time O(n^2) quadratic time O(c^n) exponential time (c is comparisons)
Selection Sort Performance
O(n^2)
Selection sort moves
O(n) moves
Selection sort comparisons
O(n^2) comparisons
Basic steps for recursive backtracking
- Define base case at top. Return true happens here
- Try first iteration
- If first iteration works, go to the next one (or return true). If first iteration fails, go back (return false/undo method happens here).
- Repeat this until the base case is reached
- If you’re not able to reach the base case, return false
Starting index for selection sort
Starting index for insertion sort
starting index for selection: 0
starting index for insertion: 1 (compares items immediately to the left, so can’t start at 0)
Radix sort performance
O(n*m)
better than n log n IF m < log n
m is number of properties to look at.
example: in 999 m would equal 3, because there are three digits to look at
Notes on quicksort
worse case O(n^2) (both comparisons and moves)
best and average case O(n log n) (both comparisons and moves)
note: performance very dependent on picking a good pivot around the median of the range
notes on mergesort
performance of n log n in every case
note: requires n additional space