CS2420 Midterm

Ace your homework & exams now with Quizwiz!

What is (approximately) the Log base 4 of 1,000,000?

10

Big O: log base 2(N)

Binary Search

Big O: N^3

CreateRandomUnique(), Max Subsequence

What happens to the overall speed of execution if you make all the code that is not the bottleneck code go 1000 times faster?

Doesn't really help, you have to fix the bottleneck code to speed it up

What is the ADT for a Car?

Gas Pedal, Brake Pedal, Steering Wheel

Which Big O category does this function use? def MakeArray(size): A = [] while len(A) < size: r = random.randrange(0,size) if r not in A: A.append(r) return A

N^3

Give real life examples of where a stack and a queue might be used.

Stack of Plates at a Restaurant, Queue is a line at a store or stack of papers on a desk waiting to be looked at, To-Do List, Email

What is (approximately) the Log base 2 of 510,000?

19

Which Big O category does this code use? For loop N times Do N3 work here. For loop N times For loop N times Do N work here. 1. N^3 2. N^4 3. N^5 4. N^6

2. N^4

Assume a CIT password must be exactly 9 characters. The first 3 characters must be a lowercase letter. The next 3 may be lower or upper case letters. The last 3 must be one of the ten digits. How many different password possibilities are there? (Just write the formula)

26*26*26*52*52*52*10*10*10

What is (approximately) 4 to the power of 15?

2^30 1,000,000,000

What is (approximately) the Log base 10 of 100,000,000?

8 (8 zeros)

Which Big O category does this code use? For loop N times For loop N times Do N3 work here Do N3 work here A. N4 B. N5 C. N6 D. N7

B. N5

Why is it more likely that Factoring might be solved in polynomial time than SAT?

Because the SAT problem is NP Complete

Give the Big-O for these algorithms. Binary Search: ... Merge Sort of mostly sorted data: ... SAT solver 'by trying all combinations': ... Multiplying two integers of N digits each: ... Selection Sort of 'random' data: ...

Binary Search: Log Base 2 (N) Merge Sort of mostly sorted data: N * Log Base 2(N) SAT solver 'by trying all combinations': 2^N Multiplying two integers of N digits each: N^2 Selection Sort of 'random' data: N^2

Assume a computer can solve the Towers Of Hanoi problem moving 1 billion disks per second. How long would it take such a computer to solve it for 50 disks? A. Way less than 1 second B. Between 1 and 1000 seconds C. Around 1,000,000 seconds D. 1,000,000,000,000,000,000 seconds or more.

C. Around 1,000,000 seconds 50 disks 10^9 moves/sec 2^50 = 10^15 moves 10^15/10^9 = 10^6

Class Stack: ?

Class Stack: def __init__(self): self.A = [] def push(self,x): self.A.append(x) def pop(self): return self.A.pop() def top(self): return self.A[-1] def empty(self): return len(self.A) == 0

If you are trying to speed up code that is running slowly, where is the bottleneck likely to be?

Find where the worst Big O is, which is where the bottleneck code will likely be. Often in nested loops

What is Big O analysis?

It's the study of how work increases as problem size increases. If something is N, the work gets 3 times more, the problem goes to N^3 and so on. It is the study of how work increases as problem size increases.

Big O: N

Linear Search

Big O: 1

List append

Big O: N*log base 2(N)

MergeSort, QuickSort

Show what happens to this array after one pass of the basic quick sort. Use the 'N' on the far left as the pivot, and use the same algorithm we studied in class. Original Array: N T A B S V C H

N T A B S V C H N A B T S V C H N A B C S V T H N A B C H V T S H A B C N V T S

Is it good practice to always optimize all your code? Explain why or why not?

No, because it would make your code unreadable and time consuming. Only optimize bottleneck code if it is running slowly.

Polynomial (Tractable) ? ---------------------------------------- Exponential (Intractable) ?

Polynomial (Tractable) 1: List append log2N: Binary Search N: Linear Search N*log2N: MergeSort N^2: BubbleSort N^3: Bad Implementation of... N^P: Nondeterministic Polynomial ---------------------------------------- Exponential (Intractable) 2^N: SAT, Towers of Hanoi 10^N/2: Factoring N!: Traveling Salesperson SAT == N^P Complete, can't be done better

Big O: N^2

SelectionSort, BubbleSort, ShakerSort, Multiply Problem (N*N)

Why is it good for a computer scientist to separate the ADT from the implementation?

So that the implementation can be changed later with minimal impact on the overall code.

Big O: 2^N

Towers of Hanoi, SAT

Big O: N!

Traveling Salesperson

When is Big O analysis most important?

When you are dealing with large amounts of data

CreateMostlySortedList assuming CreateRandomList is a working functions and using built in Python sort

def CreateMostlySortedList(size): A = CreateRandomList(size) A.sort() A[0], A[-1] = A[-1], A[0] return A

def traverse(sList): ?

def traverse(sList): age = 0 for i in range(len(sList)): age += int(sList[i].getage()) average = float(age/len(sList)) print(average)

Big O: N^10/2

factoring problem

List 5 Big O categories and an algorithm that fits into each log2(N): ... N: ... N*log2(N): ... N^2: ... N^3: ...

log2(N): Binary Search N: Linear Search N*log2(N): MergeSort N^2: BubbleSort N^3: CreateRandomUnique


Related study sets

Economics of corporate finance exam 2 study guide

View Set

POL 271- Ch. 9: International Political Economy

View Set