I have written a program with a class abstractSort that is to sort an array and count the number of times through the counter. Program 1 runs and counts, but does not sort the random list from low to high. Program 2 does better at the sort but does not count the times through the loop to sort it.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
usingnamespace std;
class AbstractSort
{
public:
virtualvoid sort(int arr[], int size) = 0;
int getComparisonCount()
{
return comparisonCount;
}
void resetComparisonCount()
{
comparisonCount= 0;
}
protected:
int compare(int x, int y);
private:
int comparisonCount;
};
// AbstractSort::compare
// Returns -1, 0, or 1 a la strcmp
// This also keeps track of the number of comparisons performed
int AbstractSort:: compare(int x, int y) //compare func
{
comparisonCount++;
return x - y;
}
// derived class
class MaxSort : public AbstractSort
{
public:
void sort(int arr[], int size);
};
//MaxSort::sort sort the given array with the given number of elements
void MaxSort::sort (int arr[], int size)
{
resetComparisonCount();
int indexOfMin;
int pass;
int j;
for ( pass = 0; pass < size - 1; pass++ )
{
indexOfMin = pass;
for ( j = pass + 1; j < size; j++ )
if ( arr[j] < arr[pass] )
indexOfMin = j;
compare (arr[j], arr[pass]);
swap ( arr[pass], arr[indexOfMin] );
}
}
// swap function for integers
void swap ( int& x, int& y )
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
constint MAX_SIZE = 100;
int arr[MAX_SIZE];
int size;
// Explain the program
cout << "This program keeps track of the number of comparisons required to\n";
cout << "to sort a randomly generated array.\n";
cout << "How large do you want the array to be (max size=100)? ";
// Get the size of the array
cin >> size;
if (size > MAX_SIZE)
{
cout << "The size of the array must be no greater than 100.";
exit(1);
}
// Initialize random number generator
srand(time(0));
// Fill the array with random numbers
for (int k = 0; k < size; k++)
{
arr[k] = rand() % 1000;
}
// Output array to be sorted
cout << "Array to be sorted is: \n";
for (int k = 0; k < size; k++)
{
cout << arr[k] << " ";
}
// Sort and output results
MaxSort maxSort1;
maxSort1.sort(arr, size);
cout << "\nThe sorted array is: \n";
for (int k = 0; k < size; k++)
{
cout << arr[k] << " ";
}
cout << "\nNumber of comparisons performed is: " << maxSort1.getComparisonCount() << endl;
system("pause");
return 0;
}
PROGRAM 2- This has a switch sort function but does not count
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
usingnamespace std;
class AbstractSort
{
public:
virtualvoid sort(int arr[], int size) = 0;
int getComparisonCount()
{
return comparisonCount;
}
void resetComparisonCount()
{
comparisonCount= 0;
}
protected:
int compare(int x, int y);
private:
int comparisonCount;
};
// AbstractSort::compare
// Returns -1, 0, or 1 a la strcmp
// This also keeps track of the number of comparisons performed
int AbstractSort:: compare(int x, int y) //compare func
{
comparisonCount++;
return x -y;
}
// derived class
class MaxSort : public AbstractSort
{
public:
void sort(int arr[], int size);
};
//MaxSort::sort sort the given array with the given number of elements
void MaxSort::sort (int arr[], int size)
{
resetComparisonCount();
for (int k = size = size-1; k>=1; k--)
{
int indexOfLargest =0;
for (int ix = 1; ix <= k; ix++)
{
if (compare (arr[ix], arr[indexOfLargest]) > 0)
{
indexOfLargest = ix;
}
}
swap(arr[indexOfLargest], arr[k]);
}
}
int main()
{
constint MAX_SIZE = 100;
int arr[MAX_SIZE];
int size;
// Explain the program
cout << "This program keeps track of the number of comparisons required to\n";
cout << "to sort a randomly generated array.\n";
cout << "How large do you want the array to be? ";
// Get the size of the array
cin >> size;
if (size > MAX_SIZE)
{
cout << "The size of the array must be no greater than 100.";
exit(1);
}
// Initialize random number generator
srand(time(0));
// Fill the array with random numbers
for (int k = 0; k < size; k++)
{
arr[k] = rand() % 1000;
}
// Output array to be sorted
cout << "Array to be sorted is: \n";
for (int k = 0; k < size; k++)
{
cout << arr[k] << " ";
}
// Sort and output results
MaxSort maxSort;
maxSort.sort(arr, size);
cout << "\nThe sorted array is: \n";
for (int k = 0; k < size; k++)
{
cout << arr[k] << " ";
}
cout << "\nNumber of comparisons performed is: " << maxSort.getComparisonCount() << endl;
system("pause");
return 0;
}