C++: Find Time Analysis Worst case O() of each member function with explanation:
1)
/** insert function:
parameters: obj
Description: inserts new elements into
the correct position in the
sorted array, sliding elements over the position to
right, as needed
**/
template <typename Object>
SortedArray::void insert(const Object &obj)
{
if (theSize >= theCapacity)
{
cout << "Error: there is no
enough memory space. " << endl;
return;
}
else
{
for (int i = ((2 * theSize) + 1);
(i >= 0 && SortedArray[i] > obj); i--)
SortedArray[i +
1] = SortedArray[i];
SortedArray[i + 1] = obj;
return (theSize + 1);
}
}
2)
/**
deleteMin function:
Description: remove the element in position 0, and
slide over allb other item
**/
templae <typename Object>
SortedArray::void deleteMin()
{
theSize--;
for (int i = 0; i < theSize; i++)
objects[i] = objects[i + 1];
}
3)
/**
Description: remove the last element in the sorted
array
*/
templae <typename Object>
void deleteMax()
{
theSize--;
}
//1
//In worst case function insert runs for :O(n) times
explanation:
if condition is satisfied , it runs for just O(1)times
but since we are considering worst case,
in else we have for loop which runs for 2*n times in worst case,
which means :2*O(n)= O(n)
//2
//worst case complexity is :O(n) where n is theSize
explanation:
since it has for loop, which runs for n times, where n is
theSize
it runs for O(n) times
//3
//worst case complexity is O(1)
explanation:
the function deleteMax has only one statement means runs in
O(1)
C++: Find Time Analysis Worst case O() of each member function with explanation: 1) /** insert function: parameters:...
write the interface of the following functions in the SortedArray.cpp, by the given functions in the SortedArray.h : #ifndef SortedArray_H #define SortedArray_H //INCLUDES #include <iostream> using namespace std; //---------------------------------- // SortedArray<Object> //---------------------------------- template <typename Object> class SortedArray { public: /** insert param: obj Description: insert new elements into the correct position in the sorted array, sliding elements over the position to right, as needed */ void insert(const Object &obj); /** Description: remove the element in position 0, and slide over...
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...
2.What is the order of complexity of the insert function in the worst case? You are required to provide a complexity function, and a proof that your complexity function belongs to a particular complexity class. In your complexity function you may specify the number of comparisons performed, as a function of input size. def insert(element, sorted): if len(sorted) == 0: return [element] else: if sorted[0] >= element: new_copy = sorted[:] new_copy.insert(0, element) return new_copy else: return [sorted[0]] + insert(element, sorted[1:])
#include <iostream> using namespace std; template <typename Item> class MyArray{ private: Item *myarray; int size; int used; void doubleSize(); public: MyArray(); ~MyArray(); int length(); void insertHead(Item i); void insertTail(Item i); void deleteHead(); void deleteTail(); void sortAscending(); void sortDescending(); Item operator [](int i){ return myarray[i]; } }; template <typename Item> MyArray<Item>::MyArray(){ size = 5; used = 0; myarray = new Item[size];...
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...
C++ There is a premade heap.h file. You will write a program that creates a heap, and then asks the user to enter values which you will put in the heap (at least 8), and then display the heap Then your program will display a menu with the following choices, and perform the appropriate actions Display the heap Add an item to the heap Remove the largest item Exit The program will continue until the user chooses to exit, at...
Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...
Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) { p = new node; p->key=x; p->left=NULL; p->right=NULL; } else { //Cheak if the element to be inserted is smaller than the root. if (x < p->key) { //call the function itself with new parameters. insert(x,p->left); } //cheak if the alement to be inserted...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended. Methods/Constructor correctly completed (2pt) This is the Map class methods Map(), put(key, value), get(key), isEmpty(), makeEmpty() Private class correctly completed (2pt) This is the Entry class methods Add the methods that are required for this to work correctly when stored in the QuadraticProbingHashTable Tester class (1pt) Show the methods of the Map...