CS 310 Final Riggins

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Complexity: 2*n+6

=(n)

Complexity: (n^2(n-1))/(n)

=(n^2)

Complexity: n^2+3*n

>(n)

Complexity: 12*n+12

>=(n)

Complexity: 3*n^3+2*n^2+12n+3

>=(n^3)

Complexity: 7*n^(4/3)

>=(nlogn)

Both Stacks and Queues can be implemented with either arrays or linked lists. Discuss the advantages and disadvtages of each implementation.

Arrays are faster and don't require extra storage space. However, they suffer front the limitation of having a fixed size. If you run out of array space, the program will fail. Linked Lists have unlimited storage capacity (up to available memory) but are slower and require extra storage for the links.

Calculation for word problems: (Ex: Number of customers triple...)

C*(n)^2 the n is how much it goes up(ex:triples =3) the algorithm could be changes from ^2 to the complexity its asking.

Doubly Linked Lists

Each node has one data item and two links, one referencing preceding node , the other the following node. -Provides two operations not available to singly linked lists. 1)lists traversed 2)Removal from tail in O(1) -used for dequeue data structure.

25n3 = ω(n3)

False

n + 2 = O(1)

False

n4/3 = O(n log(n))

False

√n = O(log(n))

False

Self-Organized Re-organization: Keep

Keep a field with the total number of accesses, and order the list on that number.

Self-Organized list

Lists are ordered based on some specific heuristic instead of sorted order, usually by frequency of access. Most frequent are at the front of the list, least frequent at rear. After each search the list is reorganized, moving just accessed node more near the front. best case complexity:O(1)

3n3 + 5n2 + 25n = O(n3)

True

3n3 + 5n2 + 25n = Θ(n3)

True

3n3 + 5n2 + 25n = Ω(n3)

True

O(n) + O(n log(n)) = O(n log(n))

True

n log10(n) = Θ(n log2(n))

True

n! = ω(n100)

True

n(n-1) / 2 = o(n3)

True

n(n-1) = Θ(n2)

True

n2 log(n) = Ω(log(n))

True

A programmer plans to use a sort algorithm known to be O(n2). The project manager states that the algorithm is OK if it will sort 2,000 records in less than one second on the target machine. The programmer does a simple test, and finds that it takes 10 milliseconds to sort 100 records. Is the algorithm fast enough?

With a O(n2) algorithm, when input size doubles, runtime time quadruples. With an array of size 1600 the time is 2 1/2 seconds. It is not fast enough.

If a certain function g(x) = Ω(n2), could it also be Θ(n2) and/or O(n2)? Explain.

Yes if the function g(x) grows at the rate n2 exactly.

A certain linear algorithm takes 2 seconds to process with an input size of n=1000 on an old desktop PC. a) How long would it take if n=2000? b) How long would it take for n=2000 if the algorithm were O(n2) instead of linear? c) How long would it take for n=2000 if the algorithm were O(n3) instead of linear?

a) A linear algorithm will take twice as long, time=4 seconds b) Four times as long, time=8 seconds c) Eight times as long, time=16 seconds

When implementing a STACK how should you implement the push(E obj)

addFirst(E obj) because a Stack is in last out first operation

When implementing a QUEUE how should you implement the enqueue(E obj)

addLast(E obj) because a Queue is in first out first operation

Complexity: n/5

anything but little o(n)

for(int x=0; x<n; x++){ for(int i=0; i<n/2; i++) system.out.println("hello"); for(int j=0; j<n/4; j++) sytem.out.println("world"); }

n(n+n) O(n^2)

When implementing a QUEUE how should you implement the dequeue(E obj)

removeFirst() because a Queue is in first out first operation

When implementing a STACK how should you implement the pop(E obj)

removeFirst() because a Stack is in last out first operation

25n2 + 14n + 2 = o(n3)

True

2n + 3 = O(n log(n))

True

2n + 3c2 = O(n)

True

2n2 = Ω(n)

True

A student is required to state whether or not a certain algorithm is O(n). The student calculates that the algorithm takes approximately 1,000,000 steps for an input size n=100. Based on this observation, the student reasons that the algorithm cannot possibly be linear, but must be much worst than linear. Is he correct?

No. There is insufficient information to determine the runtime complexity. The fact that the algorithm takes many steps with n=100 tells us nothing about how many steps it will take when n grows larger. The amount of time it takes with a specific input size is irrelevant.

Complexity: 4*c^2+12

O(1)

Dequeue one element from a queue= O(?)

O(1)

Enqueue one item into a Queue= O(?)

O(1)

Insert one item into an unordered array.

O(1)

Push one item on a stack.

O(1)

Push one item onto a stack= O(?)

O(1)

h) Find the max key in an ordered array.

O(1)

private String bar(int n) { if(n < 0) return null; if(n==1) return "A"; else if(n==2) return "B"; else if(n==3) return "C"; else if(n==4) return "D"; ... else if(n==26) return "Z"; else return null; }

O(1)

private int foo() { int count=0; for(int i=0; i < n; i++) if( i > 10) break; else count++; return count; }

O(1)

while(n > 0) { System.out.println("HELLO WORLD"); n /= 2; }

O(log n)

Skip Lists Complexity

O(log n) add,search,delete

Search for one specific item in an ordered array= O(?)

O(logn)

Complexity to remove something specific from stack

O(n)

Complexity: 25*c^2+3*n

O(n)

Delete 100 specific items from an Ordered array= O(?)

O(n)

Delete a key from an ordered array.

O(n)

Delete a key from an unordered array.

O(n)

Delete one specific item from an ordered linked list= O(?)

O(n)

Delete one specific item from an unordered array= O(?)

O(n)

Delete the largest item in an unordered array= O(?)

O(n)

Dequeue n items from a standard queue.

O(n)

Find the largest item in an unordered linked list= O(?)

O(n)

Find the max key in an unordered array.

O(n)

Insert 100 items into an ordered linked list= O(?)

O(n)

Insert n items into an unordered array= O(?)

O(n)

Insert one item into index [0] in an unordered array= O(?)

O(n)

Print the contents of an ordered array= O(?)

O(n)

Push n items onto a stack.

O(n)

Search for a specific item in an unordered Linked List= O(?)

O(n)

Search for one item in an ordered linked list.

O(n)

Search for one item in an unordered array.

O(n)

Search for one item in an unordered linked list.

O(n)

private int foo() { int count = 0; for(int i=0; i < n; i++) count++; for(int j=0; j < n; j++) count++; for(int k=0; k < n; k++) count++; return count; }

O(n)

Complexity: nlogn

O(n*(squareroot(n))

Insert n items into an ordered array.

O(n2)

Insert n items into an ordered Linked List= O(?)

O(n^2)

Complexity: O(n^2)*O(n)

O(n^3)

for(int i=0; i<n/2; i++) for(int j=0; j<i; j++) for(int k=0; k<j; k++) x++;

O(n^3)

Complexity: 5*n^2+3*n^2

O(n^4)

for(int i=0; i < n; i++) for(int j=0; j < n; j++) for(int k=0; k < n; k++) for(int l=0; l < n; l++) for(int m=0; m < n; m++) System.out.println("Hello world");

O(n^5)

Complexity to insert n items into a skip list

O(nlogn)

for(int i=1; i<=n; i*=2) for(int j=1; j <=i; j++){ x++; j++; }

O(nlogn)

int x=n; for(int i=0; i<n/10000; i++){ while(x>0){ system.out.println("hello world"); x /=2; } }

O(nlogn)

int x=n; while(x>0){ for(int i=0; i<n; i++) system.out.println("hello world!"); x=x/5; }

O(nlogn)

Self-Organized Re-organization: Remove

Remove the accessed node from the list and reinsert it at the head of the list

Skip List

Singly linked, always ordered. Lists have a variable number of forward links. All have at least one. -1/2 have two forward links -1/4 have three forward -1/8 four forward links additional storage space

Self-Organized Re-organization: Swap

Swap the accessed node with its immediate processor

What is a Priority Queue, and how does it differ from a standard queue?

The order of insertion is a secondary key in the ordering. The primary key is the priority.

1,000,000n = Θ(n)

True

2.00000001 n = ω(n)

True

25 = o(n)

True


Set pelajaran terkait

Adaptive Quizzing- Care of the Newborn

View Set

Unit 11: Add and Subtract Fractions with Unequal Denominators

View Set

Cardiac output and venous return

View Set

AP Computer Science Semester 2 Final Exam

View Set