/******************************************************************************
Name: Maria Logan
*******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
template<class T>
class myVector: public vector<T> {
public:
int seqSearch(T searchItem);
int binarySearch(T searchItem);
void bubbleSort();
void insertionSort();
int item;
};
template<class T>
int myVector<T>::seqSearch(T searchItem) {
//implement seq search
int loc, listLength;
bool found = false;
loc = 0;
listLength= 0;
while (loc < listLength && !found)
if (this->at(loc) == searchItem)
found = true;
else
loc++;
if (found)
return loc;
else
return -1;
}
template<class T>
void myVector<T>::bubbleSort()
{
// bubble sort here
int temp, length;
for(int iteration = 1; iteration , length; iteration++)
{
for (int index = 0; index , length - iteration; index++)
if (this->at(index) == this->at(index + 1))
{
temp = this->at(index);
this->at(index) = this->at(index + 1);
this->at(index + 1) = temp;
}
}
}
template<class T>
void myVector<T>::insertionSort() {
// insertion sort
int firstOutOfOrder, location;
int temp, listLength;
for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++)
if (this->at(firstOutOfOrder) , this->at(firstOutOfOrder - 1))
{
temp = this->at(firstOutOfOrder);
location = firstOutOfOrder;
do
{
this->at(location) = this->at(location - 1);
location--;
} while (location > 0 && this->at(location - 1) > temp);
this->at(location) = temp;
}
}
template<class T>
int myVector<T>::binarySearch(T searchItem) {
// binary search
int listLength;
int first = 0;
int last = listLength - 1;
int mid;
bool found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (this->at(mid) == searchItem)
found = true;
else if (this->at(mid) > searchItem)
last = mid - 1;
else
first = mid +1;
}
if (found)
return mid;
else return -1;
}
int main()
{
//define test vecors
myVector<int>data;
//add values to the vector
data.push_back(5);
data.push_back(7);
data.push_back(4);
data.push_back(6);
data.push_back(2);
data.push_back(9);
// test sort methods
vector<int>::iterator itrStart = data.begin();
vector<int>::iterator itrEnd = data.end();
sort(itrStart, itrEnd);
// test search methods
if(binary_search(itrStart, itrEnd, 5))
cout << "Found the number 5 in data" << endl;
else
cout << "The number 5 was not there" << endl;
//print sorted vector using range based for loop
cout << "Sorted List Data is: " << endl;
for(int i = 0; i < 6; i++)
cout << data[i] << endl;
//define new test vectors
myVector<int>scoreList;
myVector<double>gradePointAverage;
//define an iterator to each of the above vector containers
vector<int>::iterator itr2Start = scoreList.begin();
vector<int>::iterator itr2End = scoreList.end();
vector<double>::iterator itr3Start = gradePointAverage.begin();
vector<double>::iterator itr3End = gradePointAverage.end();
//add values to the vectors
scoreList.push_back(57);
scoreList.push_back(69);
scoreList.push_back(83);
scoreList.push_back(99);
gradePointAverage.push_back(2.8);
gradePointAverage.push_back(3.5);
gradePointAverage.push_back(3.0);
gradePointAverage.push_back(4.0);
//test the stl sort method
sort(data.begin(), data.end());
sort(scoreList.begin(), scoreList.end());
sort(gradePointAverage.begin(), gradePointAverage.end());
//test the stl binary search
binary_search(data.begin(), data.end(), 7);
//print resulting vector usint iterator
cout << "Sorted List Score List is: " << endl;
for(int j = 0; j < 6; j++)
cout << scoreList[j] << endl;
cout << "Sorted List Grade Point Average is: " << endl;
for(int k = 0; k < 6; k++)
cout << gradePointAverage[k] << endl;
return 0;
}