Data structures and algorithms Flashcards

1
Q

Characteristics of an Abstract Data Type (ADT)

A

Specifies an interface, not an implementation. While it must respond to method calls, implementation is not important.

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

Bag ADT

A
  • add
  • remove
  • contains
  • numItems
  • grab
  • toArray
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Formula for comparisons in selection sort

A

O(n^2)

This will be the efficiency regardless of input

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

Formula for moves in selection sort

A

O(n)

This will be the efficiency regardless of input

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

Radix Sort

A

-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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Bubble Sort Performance

A

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

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

Algorithm performance classes

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Selection Sort Performance

A

O(n^2)

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

Selection sort moves

A

O(n) moves

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

Selection sort comparisons

A

O(n^2) comparisons

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

Basic steps for recursive backtracking

A
  1. Define base case at top. Return true happens here
  2. Try first iteration
  3. If first iteration works, go to the next one (or return true). If first iteration fails, go back (return false/undo method happens here).
  4. Repeat this until the base case is reached
  5. If you’re not able to reach the base case, return false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Starting index for selection sort

Starting index for insertion sort

A

starting index for selection: 0

starting index for insertion: 1 (compares items immediately to the left, so can’t start at 0)

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

Radix sort performance

A

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

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

Notes on quicksort

A

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

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

notes on mergesort

A

performance of n log n in every case

note: requires n additional space

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

notes on insertion sort

A

starts at index of 1

17
Q

notes on shell sort

A

performance is generally about O(n^1.5), although it’s hard to precisely measure

performance highly dependent on a good gap sequence (increment values)

18
Q

Preorder traversal of a tree

A

visit root
recursively visit left side
recursively visit right side

19
Q

Postorder traversal

A

recursively visit left side
recursively visit right side
visit root

20
Q

Inorder traversal

A

recursively visit left side
visit root
recursively visit right side

21
Q

level-order traversal

A

visit from top to bottom, left to right

visits one level at a time