Analyzing Algorithms Flashcards

1
Q

What is the purpose of algorithm analysis?

A

To compare the effectiveness of algorithms without having to consider implementation details, the speed of the computer system used, and test data. As n grows, how does the number of operations grow?

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

How do you analyze the speed of an algorithm?

A
  • count the number of steps or operations needed
  • look at it as a function of the size of the problem
  • you can look at either the average number of steps, or the
    maximum number of steps
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the speed of a linear search?

A

O(n)

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

What is the speed of a binary search?

A

O(log(n))

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

What happens when you double n with the different typical bounds?

A

O(log n): adds c to time
O(n): double the time
O(nlogn): not much slower than O(n)
O(n^2): time4
O(n^3): time
8
O(2^n): time^2
O(n!): don’t even try

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

What is the speed of an ordered insert?

A

O(n): only one loop, goes through one element at a time, essentially a linear search

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

What is the speed of our simple sorting algorithms?

A

O(n^2): contains two nested loops

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

What is an example of a better sorting algorithm?

A

Merge sort:
- split the array into two small arrays
- sort the two halves (using two merge sorts)
- merge the two sorted halves into a sorted array after the
2 merge sorts have returned

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

What is the speed of a merge sort?

A

O(nlogn)

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