//Ryan Shahriyarpour CS1A Chapter 8 Question 9
/*****************************************************************************
*
* Sorting Benchmarks
*
*********************
*
* This program uses two identical arrays of 20 integers. One is sorted with
* a bubble sort and the other with a selection sort. Each sort counts the
* number of exchanges (swaps) it makes so the two methods can be compared.
*
* *******************
* Bubble sort - swaps neighboring elements that are out of order and
* repeats until a full pass makes no swaps.
* Selection sort - each pass finds the smallest unsorted value and swaps
* it into place (only counted if it actually moves).
*
* INPUT
* None (the arrays are stored in the program)
*
* OUTPUT
* The sorted array
* The number of exchanges made by the bubble sort
* The number of exchanges made by the selection sort
*
****************************************************************************/
#include <iostream>
using namespace std;
// Constant for the number of array elements
const int SIZE = 20;
// Function prototypes (the sorts return the number of exchanges made)
int bubbleSort(int array[], int size);
int selectionSort(int array[], int size);
void displayArray(const int array[], int size);
int main()
{
// data dictionary
int bubbleArray[SIZE] = { // Array sorted with the bubble sort
45, 12, 89, 3, 67, 24, 91, 8, 56, 33,
72, 19, 5, 98, 41, 27, 60, 14, 83, 38
};
int selectArray[SIZE] = { // Identical array for the selection sort
45, 12, 89, 3, 67, 24, 91, 8, 56, 33,
72, 19, 5, 98, 41, 27, 60, 14, 83, 38
};
int bubbleExchanges; // Exchanges made by the bubble sort
int selectExchanges; // Exchanges made by the selection sort
// Sort both arrays
bubbleExchanges = bubbleSort(bubbleArray, SIZE);
selectExchanges = selectionSort(selectArray, SIZE);
// Display the results
cout << "Sorted array: ";
displayArray(bubbleArray, SIZE);
cout << "\nBubble sort exchanges: " << bubbleExchanges << endl;
cout << "Selection sort exchanges: " << selectExchanges << endl;
return 0;
}
// Bubble sort in ascending order; returns the number of exchanges
int bubbleSort(int array[], int size)
{
// data dictionary
int index; // Loop counter comparing neighbors
int temp; // Holds a value during a swap
int exchanges = 0; // Number of swaps made
bool swapped; // True if a swap happened this pass
do
{
swapped = false;
for (index = 0; index < size - 1; index++)
{
if (array[index] > array[index + 1])
{
temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
swapped = true;
exchanges++;
}
}
} while (swapped);
return exchanges;
}
// Selection sort in ascending order; returns the number of exchanges
int selectionSort(int array[], int size)
{
// data dictionary
int startScan; // Position being filled on this pass
int index; // Loop counter that scans for the smallest
int minIndex; // Index of the smallest value found
int minValue; // The smallest value found
int exchanges = 0; // Number of swaps made
for (startScan = 0; startScan < size - 1; startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for (index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
if (minIndex != startScan)
{
array[minIndex] = array[startScan];
array[startScan] = minValue;
exchanges++;
}
}
return exchanges;
}
// Prints the array on one line
void displayArray(const int array[], int size)
{
// data dictionary
int index; // Loop counter
for (index = 0; index < size; index++)
cout << array[index] << " ";
cout << endl;
}