Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Can you help me with my code? It is only giving me one error saying my "identifier: binary_search is unidentified", and I can't figure out how to fix it.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <class T>
class myVector : public vector<T> {
bool sorted = false; // static member to remember if the list is sorted

public:
int seqSearch(T searchItem);
int binarySearch(T searchItem);
void bubbleSort();
void insertionSort();
};
int main() {
// Define test vector
myVector<string> nameList;

// Add values to the vector
nameList.push_back("Alice");
nameList.push_back("Bob");
nameList.push_back("Eve");
nameList.push_back("Charlie");
nameList.push_back("Dave");

// Test sort methods
nameList.bubbleSort();
nameList.insertionSort();

// Test search methods
int index = nameList.seqSearch("Charlie");
cout << "Charlie was found at index: " << index << endl;

index = nameList.binarySearch("Charlie");
cout << "Charlie was found at index: " << index << endl;

// Print sorted vector using range based for loop
cout << "Sorted vector: ";
for (const auto& name : nameList) {
cout << name << " ";
}
cout << endl;

// Define new test vector
myVector<int> intList;

// Define an iterator to the vector
auto it = intList.begin();

// Add values to the vector
intList.insert(it, 1);
intList.insert(it, 3);
intList.insert(it, 2);
intList.insert(it, 5);
intList.insert(it, 4);


// Test the STL binary_search algorithm
bool found = binary_search(intList.begin(), intList.end(), 3);
if (found) {
cout << "3 was found in the vector." << endl;
}
else {
cout << "3 was not found in the vector." << endl;
}

// Print the resulting vector using an iterator
cout << "Sorted vector: ";
for (it = intList.begin(); it != intList.end(); it++) {
cout << *it << " ";
}
cout << endl;

return 0;
}

template <class T>
int myVector<T>::seqSearch(T searchItem) {
// Sequential search implementation
for (int i = 0; i < this->size(); i++) {
if (this->at(i) == searchItem) {
return i; // return index of found item
}
}
return -1; // return -1 if item not found
}

template <class T>
void myVector<T>::bubbleSort() {
// Bubble sort implementation
bool swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < this->size() - 1; i++) {
if (this->at(i) > this->at(i + 1)) {
// Swap values
T temp = this->at(i);
this->at(i) = this->at(i + 1);
this->at(i + 1) = temp;
swapped = true;
}
}
}
sorted = true; // set sorted to true after sorting
}

template <class T>
void myVector<T>::insertionSort() {
// Insertion sort implementation
for (int i = 1; i < this->size(); i++) {
T value = this->at(i);
int j = i - 1;
while (j >= 0 && this->at(j) > value) {
this->at(j + 1) = this->at(j);
j--;
}
this->at(j + 1) = value;
}
sorted = true; // set sorted to true after sorting
}

template <class T>
int myVector<T>::binarySearch(T searchItem) {
// Binary search implementation
if (!sorted) { // check if list is sorted
bubbleSort(); // sort list if not sorted
}

int low = 0;
int high = this->size() - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (this->at(mid) == searchItem) {
return mid; // return index of found item
}
else if (this->at(mid) > searchItem) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return -1; // return -1 if item not found
}

int main() {
// Define test vector
myVector<string> nameList;

// Add values to the vector
nameList.push_back("Alice");
nameList.push_back("Bob");
nameList.push_back("Eve");
nameList.push_back("Charlie");
nameList.push_back("Dave");
}

 
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education