Question: Question: Time Analysis Worst case O() of each member function with explanation. Code: //SortedArray.cpp #ifdef SortedArray_H //initial size of empty array const int initSize =

Question:

Time Analysis Worst case O() of each member function with explanation.

Code:

//SortedArray.cpp #ifdef SortedArray_H

//initial size of empty array const int initSize = 0;

//Constructor template SortedArray::SortedArray() : sizeOfArray(initSize), capacityOfArray(initSize + SPARE_CAPACITY) { objectsInArray = new Object[capacityOfArray]; }

//Copy Constructor template SortedArray::SortedArray(const SortedArray &from) : objectsInArray(NULL) { operator=(from); }

//Destructor template SortedArray::~SortedArray() { delete[] objectsInArray; }

// Overloading assignment operator template const SortedArray& SortedArray:: operator= (const SortedArray &from) { if (this != &from) { delete[] objectsInArray; sizeOfArray = from.size(); capacityOfArray = from.capacity(); objectsInArray = new Object[capacity()]; for (int i = 0; i < sizeOfArray; ++i) { objectsInArray[i] = from.objectsInArray[i]; } } return *this; }

//accessor Operator template const Object & SortedArray::operator[](int index) const { return objectsInArray[index]; }

//check if two sorted arrays are same template bool SortedArray::equals(const SortedArray &rightHandSize) { if (size() == rightHandSize.size()) { for (int i = 0; i < sizeOfArray; ++i) { if (objectsInArray[i] != rightHandSize.objectsInArray[i]) return false; } return true; } return false; }

//check if array is empty template bool SortedArray:: empty() const { if (sizeOfArray == 0) return true; return false; }

//get the size of objects template int SortedArray::size() const { return sizeOfArray; }

//get the capacity of objects template int SortedArray::capacity() const { return capacityOfArray; }

//Reserve function template void SortedArray::reserve(int newCapacity) { if (newCapacity > sizeOfArray) { Object *old = objectsInArray; objectsInArray = new Object[newCapacity]; for (int i = 0; i < sizeOfArray; ++i) { objectsInArray[i] = old[i]; } capacityOfArray = newCapacity; delete[] old; } }

//function to elements of the array template void SortedArray:: print(ostream &out, char delimiter) const { for (int i = 0; i < sizeOfArray; ++i) { out << objectsInArray[i] << delimiter; } }

// function to clear the elements in the array template void SortedArray::clear() { for (int i = 0; i < sizeOfArray; ++i) { objectsInArray[i] = NULL; } sizeOfArray = 0; }

// function to inserts elements into the sorted arry template void SortedArray::insert(const Object &obj) { if(sizeOfArray == capacityOfArray) { reserve(2 * sizeOfArray + 1); }

if(sizeOfArray == 0) { objectsInArray[0] = obj; } else { int i; for (i= sizeOfArray - 1; (i >= 0 && objectsInArray[i] > obj); i--) objectsInArray[i + 1] = objectsInArray[i];

objectsInArray[i + 1] = obj; } sizeOfArray++; }

//functino to delete minimum value template void SortedArray::deleteMinimum() { for(int i = 0; i < sizeOfArray - 1; ++i) { objectsInArray[i] = objectsInArray[i + 1]; } sizeOfArray--; }

//function to delete maximum value template void SortedArray::deleteMaximum() { sizeOfArray--; }

//function to find minimum value template const Object & SortedArray::findMinimum() const { return objectsInArray[0]; }

//function to find maximum value template const Object & SortedArray::findMaximum() const { return objectsInArray[sizeOfArray - 1]; }

//Binary Search function template int binarySearch(Object *array, int left, int right, Object object) { if (right >= left) { int mid = left + (right - left) / 2;

if (array[mid] == object) return mid;

if (array[mid] > object) return binarySearch(array, left, mid - 1, object);

return binarySearch(array, mid + 1, right, object); } return -1; }

//Binary Search template int SortedArray::binarySearch(const Object &obj) { return binarySearch(objectsInArray, 0, sizeOfArray, obj); }

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!