selectionSort.txt
Info: To get a list of random numbers for your own tests please visit: random.org
/* Sorting Benchmarks - This program uses two identical arrayys of at
least 20 integers. It calls a function that uses the bubble sort
algorithm to sort one of the arrays in ascending order. The function
keeps a count of the number of exchanges it makes. The program then
calls a function that uses the selection sort algorithm to sort the
other array. It keeps a count of the number of exchanges it makes.
The values are displayed on the screen. */
#include "Utility.h"
/* Function prototypes */
int getFileBSort(int[], int);
int bubbleSortAlg(int[], const int);
void displaySorted(const int[], const int[], const int);
void displayUnsorted(const int[], const int[], const int);
int getFileSSort(int[], int);
int selectionSortAlg(int[], int);
void displayComparison(const int, const int, const int);
int main()
{
const int SORT_NUMELS = 20;
/* These arrays hold the numbers stored in "bubbleSort.txt"
and "selectionSort.txt" */
int bubbleSort[SORT_NUMELS] = { };
int selectionSort[SORT_NUMELS] = { };
int returnCode = 0,
numSwapsBSort = 0,
numSwapsSSort = 0;
/* Get and store the numbers in bubbleSort */
returnCode = getFileBSort(bubbleSort, SORT_NUMELS);
/* If returnCode is not -1 after opening and processing "bubbleSort.txt",
"selectionSort.txt" is tried next. */
if (returnCode != -1)
{
/* Get and store the numbers in selectionSort */
returnCode = getFileSSort(selectionSort, SORT_NUMELS);
/* If opening "selectionSort.txt" fails, an error message is displayed,
and the following code-block will not be processed. */
if (returnCode != -1)
{
/* Displays the unsorted list of numbers contained in both arrays */
displayUnsorted(bubbleSort, selectionSort, SORT_NUMELS);
/* Sorts the numbers and returns the number of swaps it has taken */
numSwapsBSort = bubbleSortAlg(bubbleSort, SORT_NUMELS);
/* Sorts the numbers and returns the number of swaps it has taken */
numSwapsSSort = selectionSortAlg(selectionSort, SORT_NUMELS);
/* Displays the sorted numbers contained in both arrays */
displaySorted(bubbleSort, selectionSort, SORT_NUMELS);
/* Displays a comparison in the number of swaps between bubble and
selection sort */
displayComparison(numSwapsBSort, numSwapsSSort, SORT_NUMELS);
}
}
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getFileBSort
This function accepts bubbleSort and SORT_NUMELS as
its arguments. It processes the content of the file
"bubbleSort.txt". If the file is opened successfully,
the numbers are read-in and stored in selectionSort. If
a file processing error occurs, a message is displayed,
and the function exits with return code -1.
********************************************************** */
int getFileBSort(int bubbleSort[], int SORT_NUMELS)
{
int count = 0;
ifstream readNums;
readNums.open("bubbleSort.txt");
if (readNums)
{
while (count < SORT_NUMELS && !readNums.eof())
{
readNums >> bubbleSort[count];
count++;
}
}
else
{
cout << "\nFile open error: The file 'bubbleSort.txt' could not\n"
<< "be opened or processed. Please make sure that the filename is\n"
<< "correct and the file is not damaged or has been moved from the\n"
<< "program folder. Please close this program now and try again ...\n\n";
return -1;
}
readNums.close();
return 0;
}
/* **********************************************************
Definition: displayUnsorted
This function accepts bubbleSort, selectionSort and
SORT_NUMELS as its arguments. It displays the unsorted
list of numbers contained in both arrays.
********************************************************** */
void displayUnsorted(const int bubbleSort[], const int selectionSort[],
const int SORT_NUMELS)
{
cout << "\n\t\tBUBBLE SORT - UNSORTED LIST\n";
for (int index = 0; index < SORT_NUMELS; index++)
{
/* This if statement adds a newline in case of sorted
lists equal to or above 50 elements */
if (index % 25 == 0)
{
cout << "\n\t";
}
cout << bubbleSort[index] << " ";
}
cout << "\n";
cout << "\n\t\tSELECTION SORT - UNSORTED LIST\n";
for (int index = 0; index < SORT_NUMELS; index++)
{
if (index % 25 == 0)
{
cout << "\n\t";
}
cout << selectionSort[index] << " ";
}
cout << "\n";
}
/* **********************************************************
Definition: bubbleSortAlg
This function accepts bubbleSort and SORT_NUMELS as its
arguments. SORT_NUMELS contains the number of elements
stored in the array. It sorts the numbers stored in the
file "bubbleSort.txt" and keeps count of the number of
swaps it takes to sort the array, and returns this value.
********************************************************** */
int bubbleSortAlg(int bubbleSort[], int SORT_NUMELS)
{
int temp = 0,
count = 0,
swapCount = 0;
bool swap = false;
do
{
swap = false;
for (int count = 0; count < (SORT_NUMELS - 1); count++)
{
if (bubbleSort[count] > bubbleSort[count + 1])
{
temp = bubbleSort[count];
bubbleSort[count] = bubbleSort[count + 1];
bubbleSort[count + 1] = temp;
++swapCount;
swap = true;
}
}
} while (swap);
return swapCount;
}
/* **********************************************************
Definition: getFileSSort
This function accepts selectionSort and SORT_NUMELS as
its arguments. It processes the content of the file
"selectionSort.txt". If the file is opened successfully,
the numbers are read-in and stored in selectionSort. If
a file processing error occurs, a message is displayed,
and the function exits with return code -1.
********************************************************** */
int getFileSSort(int selectionSort[], int SORT_NUMELS)
{
int count = 0;
ifstream readNums;
readNums.open("selectionSort.txt");
if (readNums)
{
while (count < SORT_NUMELS && !readNums.eof())
{
readNums >> selectionSort[count];
count++;
}
}
else
{
cout << "\nFile open error: The file 'selectionSort.txt' could not\n"
<< "be opened or processed. Please make sure that the filename is\n"
<< "correct and the file is not damaged or has been moved from the\n"
<< "program folder. Please close this program now and try again ...\n\n";
return -1;
}
readNums.close();
return 0;
}
/* **********************************************************
Definition: selectionSortAlg
This function accepts selectionSort and SORT_NUMELS as
its arguments. SORT_NUMELS contains the number of
elements stored in the array. It sorts the numbers stored
in the file "selectionSort.txt" and keeps count of the
number of swaps it takes to sort the array, and returns
this value.
********************************************************** */
int selectionSortAlg(int selectionSort[], int SORT_NUMELS)
{
int startScan = 0,
minIndex = 0,
minValue = 0,
index = 0,
swapCount = 0;
bool swap = false;
for (startScan = 0; startScan < SORT_NUMELS - 1; startScan++)
{
swap = false;
minIndex = startScan;
minValue = selectionSort[startScan];
for (index = startScan + 1; index < SORT_NUMELS; index++)
{
if (selectionSort[index] < minValue)
{
minValue = selectionSort[index];
minIndex = index;
swap = true;
}
}
selectionSort[minIndex] = selectionSort[startScan];
selectionSort[startScan] = minValue;
if (swap == true)
{
swapCount += 1;
}
}
return swapCount;
}
/* **********************************************************
Definition: displaySorted
This function accepts bubbleSort, selectionSort and
SORT_NUMELS holding the number of elements in the arrays
as its arguments. It displays the sorted list of n-numbers
contained in the arrays in sorted order.
********************************************************** */
void displaySorted(const int bubbleSort[], const int selectionSort[],
const int SORT_NUMELS)
{
cout << "\n\t\tBUBBLE SORT - SORTED LIST\n";
for (int index = 0; index < SORT_NUMELS; index++)
{
/* This if statement adds a newline in case of sorted
lists equal to or above 50 elements */
if (index % 25 == 0)
{
cout << "\n\t";
}
cout << bubbleSort[index] << " ";
}
cout << "\n";
cout << "\n\t\tSELECTION SORT - SORTED LIST\n";
for (int index = 0; index < SORT_NUMELS; index++)
{
if (index % 25 == 0)
{
cout << "\n\t";
}
cout << selectionSort[index] << " ";
}
cout << "\n";
}
/* **********************************************************
Definition: displayComparison
This function accepts bubbleSort, selectionSort and
SORT_NUMELS holding the number of elements in the arrays
as its arguments. It displays the number of swaps it has
taken bubble sort and selection sort to sort a list of
n-numbers.
********************************************************** */
void displayComparison(const int numSwapsBSort, const int numSwapsSSort,
const int SORT_NUMELS)
{
cout << "\n\n\t\t\t NUMBER OF SWAPS:\n"
<< "\t\t\t " << SORT_NUMELS << " NUMBERS\n\n"
<< "\t\tBUBBLE SORT" << "\t\t" << "SELECTION SORT\n"
<< "\t\t-----------" << "\t\t" << "--------------\n";
cout << "\t\t" << numSwapsBSort << "\t\t\t" << numSwapsSSort;
}
No comments:
Post a Comment