Question

1. State and explain the definition of big-O. 2. Explain why we use big-O to compare...

1. State and explain the definition of big-O.
2. Explain why we use big-O to compare algorithms.
3. Explain why binary search runs in O(log n) time.
4. Under what conditions is it possible to sort a list in less than O(nlog n) time?
5. List and explain the worst-case and average-case running times for each Vector method below:
(a) insert(iterator here, Object item)

(b) insertAtHead

(c) insertAtTail (aka push back)

(d) get(iterator here)

(e) get(index i)

(f) remove(iterator here)

(g) remove(index i)

(h) splice(iterator place here, iterator from here, iterator to here)
(Be sure you understand when (and why) push back runs in constant average time.)


6. List and explain the worst-case and average-case running times for each LinkedList method below:
(a) insert(iterator here, Object item)

(b) insertAtHead

(c) insertAtTail (aka push back)

(d) get(iterator here)

(e) get(index i)

(f) remove(iterator here)

(g) remove(index i)

(h) splice(iterator place here, iterator from here, iterator to here)
7. When should you use a Vector, and when should you use a Linked List?

8. Assume you have a singly linked list with no tail pointer. Implement removeTail(). Raise an exception of the method is called on an empty list.
template<typename Object> class LinkedList {
private:

class Node {

Object data;

Node* next;

};

Node *head;
public:

LinkedList() : head(nullptr) {}

Object removeTail(Object data);

};


9. What are iterators? What purpose do they serve?
10. What does it mean to invalidate an iterator?
11. Explain the difference between separate chaining and open addressing in hash tables.
12. Define load factor and explain its relevance to hash table performance.
13. What are collisions with respect to hash tables?
14. Which hash tables distinguish between slots that have never been used, and slots that once contained an item but has now been deleted.
15. List and explain the worst-case and average-case running times for each HashTable method below
(a) void insert(Key k, Value v)

(b) bool contains(Key k)

(c) Value get(Key k)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.Definition: A theoretical measure of the execution of an algorithm, usually the time or memory needed, given the problem size n, which is usually the number of items. Informally, saying some equation f(n) = O(g(n)) means it is less than some constant multiple of g(n). The notation is read, "f of n is big oh of g of n".

Formal Definition: f(n) = O(g(n)) means there are positive constants c and k, such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k. The values of c and k must be fixed for the function f and must not depend on n.
Big O notation is a convenient way to describe how fast a function is growing.With Big O Notation we express the runtime in terms of — how quickly it grows relative to the input, as the input gets larger.

cg (n) f(n)

2.

Big O Notation helps to identify

  1. how quickly the runtime grows — Being that it is difficult to determine the exact runtime of an algorithm. It depends on the speed of the computer processor. So instead of talking about the runtime directly, we use Big O Notation to talk about how quickly the runtime grows.
  2. relative to the input —With Big O Notation, we use the size of the input, which we call “n”. So we can say things like the runtime grows “on the order of the size of the input” (O(n)) or “on the order of the square of the size of the input” (O(n²)).
  3. as the input gets larger — Our algorithm may have steps that seem expensive when n is small but are eclipsed eventually by other steps as n gets larger. For Big O Notation analysis, we care more about the stuff that grows fastest as the input grows, because everything else is quickly eclipsed as n gets very large.

3.

Calculating Time complexity:

  • Let say the iteration in Binary Search terminates after k iterations.
  • At each iteration, the array is divided by half. So let’s say the length of array at any iteration is n
  • At Iteration 1,
    Length of array = n
  • At Iteration 2,
    Length of array = n2
  • At Iteration 3,
    Length of array = (n2)2 = n22
  • Therefore, after Iteration k,
    Length of array = n2k
  • Also, we know that after
    After k divisions, the length of array becomes 1
  • Therefore
    Length of array = n2k = 1
    => n = 2k
    
  • Applying log function on both sides:
    => log2 (n) = log2 (2k)
    => log2 (n) = k log2 (2)
  • Hence, the time complexity of Binary Search is

    log2 (n)

  • As (loga (a) = 1)
    Therefore,
    => k = log2 (n)
    

4.Under what conditions is it possible to sort a list in less than O(nlog n) time?

If the list is already sorted ,it is possible to sort a list in less than O(nlog n) time?

Add a comment
Know the answer?
Add Answer to:
1. State and explain the definition of big-O. 2. Explain why we use big-O to compare...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • Use Java language You may assume that you always map strings to integers, and therefore you...

    Use Java language You may assume that you always map strings to integers, and therefore you need not worry about using generics. Here are the methods that your map must implement. void put (String key, Integer value); insert this key-value pair into the map under the following rules: If key already exists in the map, change its value to value, overwriting what was there If key is not already there, insert this key-value pair into the map. boolean containsKey(String key);...

  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT