For which of the following operations does std::list have a slower worst-case complexity than std::vector?
begin |
||
push_back |
||
erase |
||
insert |
||
operator= |
||
size |
Select all that apply
Answer
push_back
operator=
size
(In most of the cases the vectors outperform the lists. This is because caching is fastee when the data is sorted)
For which of the following operations does std::list have a slower worst-case complexity than std::vector? begin...
1.If a list is implemented as a singly linked stack, give the big-O worst-case time complexity of the following operations (as usual use the smallest standard big-O category that works: a) push_front, b) push_back, c) lookup, d) read the i'th member 2.Repeat question 3 for a dynamic array (for example, as in the C++ vector class)
2.1 Given an unsorted std::vector<int> and a number n, what is the worst-case time complexity for finding the pair of integers whose sum is closest to n, using no additional memory? For example, given the vector {12, 3, 17, 5, 7} and n = 13, we would get the pair {5, 7}.
Complete the following table by identifying the Big-O
complexity of the operation when performed on each data structure
as implemented by the C++ Standard Template Library (STL). Select
"Not Available" if the STL does not support the
operation.
This is only for std::list<T> (Doubly Linked
List)
Operation Description std::array<T> (Fixed Sized Vector) std::vector<T> (Extendable Sized Vector) std::forward_list<T> (Singly Linked List) std::list<T> (Doubly Linked List) default construction create container with no arguments empty checks whether the container is empty size returns...
Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...
1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...
Question 4 (10 marks) When analysing the complexity of algorithms, there are three main approaches: worst case, best case and average case. As an example, consider measuring the complexity of list-merging by counting the number of comparisons used As a test example, assume the following A1: There are two ordered lists, each of length 4, say A2: Neither list contains repeats, so a! < a2 < аз < a4 and bl <b2 < b3 < b4 A3: The lists are...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
02. Log N Vector
Due Sunday by 11:59pm
Points 149
O(log(N)) Vector
std::vector is pretty cool but it has one big problem: every
once in a while, push_back has to create a whole new array and copy
a bunch of elements which is O(n)! Luckily, we can do better, in
terms of Big-O. The goal of this homework is to write a class that
behaves like a vector but without the O(n) push_back.
Whenever we run out of space, we'll...