// sorting template program
sortTemp.cpp
#include <iostream.h>
#include <iomanip.h>
#include <dice.h>
#include <apvector.h>
const int SIZE = 500;
void fillArray (apvector<int> &temp);
void screenOutput (const apvector<int> &temp);
void swap (int &a, int &b);
void bubbleSort (apvector<int> &list);
void selectionSort (apvector<int> &list);
void insertionSort (apvector<int> &list);
void mergeSort(apvector<int> &list);
void quickSort (apvector<int> &list);
void sortMenu (apvector<int>
&temp);
main()
{
apvector<int> list(SIZE+1);
sortMenu(list);
return 0;
}
void fillArray (apvector<int> &temp)
/* Asks the user for two inputs: 1) the number of data to generate, and
2) the largest possible random integer to create. Then proceeds to fill
the array, from 1..number, where number is stored in temp[0]. */
{
int size;
cout << "How many numbers to you wish to generate? ";
cin >> temp[0];
cout << endl << "Largest integer to generate? ";
cin >> size;
dice die(size); // allocate dice object
for (int loop=1; loop<=temp[0]; loop++)
temp[loop] = die.roll();
}
void screenOutput (const apvector<int> &temp)
// prints out the contents of the array in tabular form, 12 columns
{
cout << setiosflags (ios::right) << endl;
for (int loop=1; loop<=temp[0]; loop++)
{
cout << setw(6) << temp[loop];
if (loop % 12 == 0) cout << endl;
}
cout << endl;
}
void swap (int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void bubbleSort (apvector<int> &list)
{
cout << endl << "Bubble Sort" << endl << endl;
}
void selectionSort (apvector<int> &list)
{
cout << endl << "Selection Sort" << endl << endl;
}
void insertionSort (apvector<int> &list)
{
cout << endl << "Insertion Sort" << endl << endl;
}
void mergeSort(apvector<int> &list)
{
cout << endl << "MergeSort" << endl << endl;
}
void quickSort (apvector<int> &list)
{
cout << endl << "QuickSort" << endl << endl;
}
void sortMenu (apvector<int> &temp)
{
char choice, print;
do
{
cout << "Sorting algorithm menu" << endl << endl;
cout << "(1) Bubble sort" << endl;
cout << "(2) Selection sort" << endl;
cout << "(3) Insertion sort" << endl;
cout << "(4) Recursive mergesort" << endl;
cout << "(5) Quicksort" << endl;
cout << "(6) Quit" << endl << endl;
cout << "Choice ---> ";
cin >> choice;
cin.get(); // to dump return key
cout << endl;
if ('1'<=choice && choice<='5')
{
fillArray(temp);
switch (choice)
{
case '1' : bubbleSort(temp); break;
case '2' : selectionSort(temp); break;
case '3' : insertionSort(temp); break;
case '4' : mergeSort(temp); break;
case '5' : quickSort(temp); break;
}
cout << endl << "Print list to screen (y/n)? ";
cin >> print;
cin.get();
if (print == 'y' || print == 'Y')
screenOutput(temp);
cout << endl << "Hit return to continue ";
cin.get();
}
}
while (choice != '6');
}