//Charlotte Davies-Kiernan CS1A Chapter 8 P. 489 #10
//
/******************************************************************************
*
* Compare Sorting Progress
* ____________________________________________________________________________
* This program demonstrates how Bubble Sort and Selection Sort operate step
* by step. Using two identical arrays of 8 integers, the program displays the
* amount of passes it took for each in order to sort them in ascending order.
* This allows you to visualize the process of the sorting progress.
* ____________________________________________________________________________
* Input
* SIZE :amount of elements in both array1 and array2
* array1 :an array with 8 integers that are identical to array2
* array2 :an array with 8 integeres that are identical to array1
* Output
* bubbleSort :funciton to sort array1
* selectionSort :funciton to sort array2
* displayArray :function to display the array
*****************************************************************************/
#include <iostream>
using namespace std;
//Function Prototypes
void displayArray(const int arr[], int size);
void bubbleSort(int arr[], int size);
void selectionSort(int arr[], int size);
int main() {
const int SIZE = 8;
int array1[SIZE] = {25, 10, 56, 3, 45, 8, 19, 30};
int array2[SIZE];
for(int i = 0; i < SIZE; i++)
array2[i] = array1[i];
cout << "Original contents of the first array (Bubble Sort): " << endl;
displayArray(array1, SIZE);
cout << "Bubble Sort Process: " << endl;
bubbleSort(array1, SIZE);
cout << "Original contents of the second array (Selection Sort): " << endl;
displayArray(array2, SIZE);
cout << "Selection Sort Process: " << endl;
selectionSort(array2, SIZE);
return 0;
}
//Function to Display Array Contents
void displayArray(const int arr[], int size){
for (int i = 0; i < size; i++)
cout << arr[i] << " " ;
cout << endl;
}
//Bubble Sort Function
void bubbleSort(int arr[], int size){
bool swapped;
for(int i = 0; i < size - 1; i++){
swapped = false;
for(int j = 0; j< size - i - 1; j++){
if(arr[j] > arr[j + 1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
cout << "After pass " << i + 1 << ": ";
displayArray(arr, size);
if(!swapped)
break;
}
}
//Selection Sort Function
void selectionSort(int arr[], int size){
int minIndex;
int temp;
for(int i = 0; i < size -1; i++){
minIndex = i;
for(int j = i + 1; j < size; j++){
if(arr[j] < arr[minIndex])
minIndex = j;
}
if(minIndex != i){
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
cout << "After pass " << i + 1 << ": ";
displayArray(arr, size);
}
}