Modify the quick sort implementation in the textbook to sort the array using pivot as the median of the first, last, and middle elements of the array. Add the modified quick sort implementation to the arrayListType class provided (arrayListType.h). Ask the user to enter a list of positive integers ending with -999, sort the integers, and display the pivots for each iteration and the sorted array. Main Function #include #include "arrayListType.h" using namespace std; int main() { arrayListType list; int num; cout << "Line 8: Enter numbers ending with -999" << endl; cin >> num; while (num != -999) { list.insert(num); cin >> num; } cout << "Line 15: The list before sorting:" << endl; list.print(); cout << endl; list.selectionSort(); cout << "Line 19: The list after sorting:" << endl; list.print(); cout << endl; return 0; } Header File (arrayList.h) Including images #include #include using namespace std; #ifndef H_arrayListType #define H_arrayListType template class arrayListType { public: const arrayListType& operator= (const arrayListType&); bool isEmpty() const; bool isFull() const; int listSize() const; int maxListSize() const; void print() const; bool isItemAtEqual(int location, const elemType& item) const; void insertAt(int location, const elemType& insertItem); void insertEnd(const elemType& insertItem); void removeAt(int location); void retrieveAt(int location, elemType& retItem) const; void replaceAt(int location, const elemType& repItem); void clearList(); int seqSearch(const elemType& item) const; void insert(const elemType& insertItem); void remove(const elemType& removeItem); arrayListType(int size = 100); arrayListType(const arrayListType& otherList); ~arrayListType(); protected: elemType* list; int length; int maxSize; }; #endif template bool arrayListType::isEmpty() const { return (length == 0); } template bool arrayListType::isFull() const { return (length == maxSize); } template int arrayListType::listSize() const { return length; } template int arrayListType::maxListSize() const { return maxSize; } template void arrayListType::print() const { for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } template bool arrayListType::isItemAtEqual (int location, const elemType& item) const { return(list[location] == item); } template void arrayListType::insertAt (int location, const elemType& insertItem) { if (location < 0 || location >= maxSize) cerr << "The position of the item to be inserted " << "is out of range" << endl; else if (length >= maxSize) cerr << "Cannot insert in a full list" << endl; else { for (int i = length; i > location; i--) list[i] = list[i - 1]; list[location] = insertItem; length++; } } template void arrayListType::insertEnd(const elemType& insertItem) { if (length >= maxSize) cerr << "Cannot insert in a full list" << endl; else { list[length] = insertItem; length++; } } template void arrayListType::removeAt(int location) { if (location < 0 || location >= length) cerr << "The location of the item to be removed " << "is out of range" << endl; else { for (int i = location; i < length - 1; i++) list[i] = list[i + 1]; length--; } } template void arrayListType::retrieveAt (int location, elemType& retItem) const { if (location < 0 || location >= length) cerr << "The location of the item to be retrieved is " << "out of range." << endl; else retItem = list[location]; } template void arrayListType::replaceAt (int location, const elemType& repItem) { if (location < 0 || location >= length) cerr << "The location of the item to be replaced is " << "out of range." << endl; else list[location] = repItem; } template void arrayListType::clearList() { length = 0; }
Modify the quick sort implementation in the textbook to sort the array using pivot as the median of the first, last, and middle elements of the array. Add the modified quick sort implementation to the arrayListType class provided (arrayListType.h). Ask the user to enter a list of positive integers ending with -999, sort the integers, and display the pivots for each iteration and the sorted array.
Main Function
#include <iostream>
#include "arrayListType.h"
using namespace std;
int main()
{
arrayListType<int> list;
int num;
cout << "Line 8: Enter numbers ending with -999" << endl;
cin >> num;
while (num != -999)
{
list.insert(num);
cin >> num;
}
cout << "Line 15: The list before sorting:" << endl;
list.print();
cout << endl;
list.selectionSort();
cout << "Line 19: The list after sorting:" << endl;
list.print();
cout << endl;
return 0;
}
Header File (arrayList.h) Including images
#include <iostream>
#include <cassert>
using namespace std;
#ifndef H_arrayListType
#define H_arrayListType
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem) const;
void replaceAt(int location, const elemType& repItem);
void clearList();
int seqSearch(const elemType& item) const;
void insert(const elemType& insertItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType(const arrayListType<elemType>& otherList);
~arrayListType();
protected:
elemType* list;
int length;
int maxSize;
};
#endif
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
(int location, const elemType& item) const
{
return(list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::insertAt
(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize)
cerr << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1];
list[location] = insertItem;
length++;
}
}
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize)
cerr << "Cannot insert in a full list" << endl;
else
{
list[length] = insertItem;
length++;
}
}
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be removed "
<< "is out of range" << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
template <class elemType>
void arrayListType<elemType>::retrieveAt
(int location, elemType& retItem) const
{
if (location < 0 || location >= length)
cerr << "The location of the item to be retrieved is "
<< "out of range." << endl;
else
retItem = list[location];
}
template <class elemType>
void arrayListType<elemType>::replaceAt
(int location, const elemType& repItem)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be replaced is "
<< "out of range." << endl;
else
list[location] = repItem;
}
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
}
Step by step
Solved in 5 steps with 7 images