//Maxwell Brewer CS1A Chapter 8, p. 488, #9
//
/*******************************************************************************
* SORTING BENCHMARKS
* _____________________________________________________________________________
* This program will compare the efficiency of Bubble Sort and Selection Sort
* by counting the number of exchanges each algorithm makes when sorting the
* same list of numbers.
* _____________________________________________________________________________
* INPUT
* None (The array of numbers is predefined).
*
* OUTPUT
* The number of exchanges made by each sorting algorithm.
*
******************************************************************************/
#include <iostream>
using namespace std;
// Bubble Sort function definition
int bubbleSort(int arr[], int size)
{
int count = 0; // Counter for number of exchanges
// Bubble Sort algorithm
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap adjacent elements if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
count++; // Increment exchange count
}
}
}
return count;
}
int selectionSort(int arr[], int size)
{
int pos, count = 0; // Variables to store minimum position
// and exchange count
// Selection Sort algorithm
for (int i = 0; i < size - 1; i++)
{
pos = i; // Assume the current element is the minimum
for (int j = i + 1; j < size; j++)
{
if (arr[pos] > arr[j])
pos = j; // Update position of minimum element
}
// Swap elements if a new minimum is found
if (pos != i)
{
int temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
count++; // Increment exchange count
}
}
return count;
}
int main()
{
// Initialize arrays with identical values for fair comparison
int arr [] = {1, 4, 6, 2, 3, 7, 8, 5, 12, 18, 14,
19, 9, 15, 10, 11, 20, 13, 17, 16};
int arr1[] = {1, 4, 6, 2, 3, 7, 8, 5, 12, 18, 14,
19, 9, 15, 10, 11, 20, 13, 17, 16};
int n = 20;
// Call sorting functions and display the number of exchanges for each
cout << "\nThe number of exchanges made in Bubble Sort is: "
<< bubbleSort(arr, n);
cout << "\n\nThe number of exchanges made in Selection Sort is: "
<< selectionSort(arr1, n);
return 0;
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICAgIENoYXB0ZXIgOCwgcC4gNDg4LCAgIzkKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogU09SVElORyBCRU5DSE1BUktTCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSB3aWxsIGNvbXBhcmUgdGhlIGVmZmljaWVuY3kgb2YgQnViYmxlIFNvcnQgYW5kIFNlbGVjdGlvbiBTb3J0CiAqIGJ5IGNvdW50aW5nIHRoZSBudW1iZXIgb2YgZXhjaGFuZ2VzIGVhY2ggYWxnb3JpdGhtIG1ha2VzIHdoZW4gc29ydGluZyB0aGUKICogc2FtZSBsaXN0IG9mIG51bWJlcnMuCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIElOUFVUCiAqICAgTm9uZSAoVGhlIGFycmF5IG9mIG51bWJlcnMgaXMgcHJlZGVmaW5lZCkuCiAqIAogKiBPVVRQVVQKICogICBUaGUgbnVtYmVyIG9mIGV4Y2hhbmdlcyBtYWRlIGJ5IGVhY2ggc29ydGluZyBhbGdvcml0aG0uCiAqCiAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBCdWJibGUgU29ydCBmdW5jdGlvbiBkZWZpbml0aW9uCmludCBidWJibGVTb3J0KGludCBhcnJbXSwgaW50IHNpemUpCnsKICAgIGludCBjb3VudCA9IDA7IC8vIENvdW50ZXIgZm9yIG51bWJlciBvZiBleGNoYW5nZXMKCiAgICAvLyBCdWJibGUgU29ydCBhbGdvcml0aG0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZSAtIDE7IGkrKykKICAgIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IHNpemUgLSBpIC0gMTsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGFycltqXSA+IGFycltqICsgMV0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIC8vIFN3YXAgYWRqYWNlbnQgZWxlbWVudHMgaWYgdGhleSBhcmUgaW4gdGhlIHdyb25nIG9yZGVyCiAgICAgICAgICAgICAgICBpbnQgdGVtcCA9IGFycltqXTsKICAgICAgICAgICAgICAgIGFycltqXSA9IGFycltqICsgMV07CiAgICAgICAgICAgICAgICBhcnJbaiArIDFdID0gdGVtcDsKICAgICAgICAgICAgICAgIGNvdW50Kys7IC8vIEluY3JlbWVudCBleGNoYW5nZSBjb3VudAogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBjb3VudDsKfQoKaW50IHNlbGVjdGlvblNvcnQoaW50IGFycltdLCBpbnQgc2l6ZSkKewogICAgaW50IHBvcywgY291bnQgPSAwOyAvLyBWYXJpYWJsZXMgdG8gc3RvcmUgbWluaW11bSBwb3NpdGlvbgogICAgICAgICAgICAgICAgICAgICAgICAvLyBhbmQgZXhjaGFuZ2UgY291bnQKCiAgICAvLyBTZWxlY3Rpb24gU29ydCBhbGdvcml0aG0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZSAtIDE7IGkrKykKICAgIHsKICAgICAgICBwb3MgPSBpOyAvLyBBc3N1bWUgdGhlIGN1cnJlbnQgZWxlbWVudCBpcyB0aGUgbWluaW11bQoKICAgICAgICBmb3IgKGludCBqID0gaSArIDE7IGogPCBzaXplOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBpZiAoYXJyW3Bvc10gPiBhcnJbal0pCiAgICAgICAgICAgICAgICBwb3MgPSBqOyAvLyBVcGRhdGUgcG9zaXRpb24gb2YgbWluaW11bSBlbGVtZW50CiAgICAgICAgfQoKICAgICAgICAvLyBTd2FwIGVsZW1lbnRzIGlmIGEgbmV3IG1pbmltdW0gaXMgZm91bmQKICAgICAgICBpZiAocG9zICE9IGkpCiAgICAgICAgewogICAgICAgICAgICBpbnQgdGVtcCA9IGFycltpXTsKICAgICAgICAgICAgYXJyW2ldID0gYXJyW3Bvc107CiAgICAgICAgICAgIGFycltwb3NdID0gdGVtcDsKICAgICAgICAgICAgY291bnQrKzsgLy8gSW5jcmVtZW50IGV4Y2hhbmdlIGNvdW50CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBjb3VudDsKfQoKaW50IG1haW4oKQp7CiAgICAvLyBJbml0aWFsaXplIGFycmF5cyB3aXRoIGlkZW50aWNhbCB2YWx1ZXMgZm9yIGZhaXIgY29tcGFyaXNvbgogICAgaW50IGFyciBbXSA9IHsxLCA0LCA2LCAyLCAzLCA3LCA4LCA1LCAxMiwgMTgsIDE0LAogICAgICAgICAgICAgICAgIDE5LCA5LCAxNSwgMTAsIDExLCAyMCwgMTMsIDE3LCAxNn07CiAgICAgICAgICAgICAgICAgCiAgICBpbnQgYXJyMVtdID0gezEsIDQsIDYsIDIsIDMsIDcsIDgsIDUsIDEyLCAxOCwgMTQsCiAgICAgICAgICAgICAgICAgMTksIDksIDE1LCAxMCwgMTEsIDIwLCAxMywgMTcsIDE2fTsKICAgIGludCBuID0gMjA7CgogICAgLy8gQ2FsbCBzb3J0aW5nIGZ1bmN0aW9ucyBhbmQgZGlzcGxheSB0aGUgbnVtYmVyIG9mIGV4Y2hhbmdlcyBmb3IgZWFjaAogICAgY291dCA8PCAiXG5UaGUgbnVtYmVyIG9mIGV4Y2hhbmdlcyBtYWRlIGluIEJ1YmJsZSBTb3J0IGlzOiAiIAogICAgICAgICA8PCBidWJibGVTb3J0KGFyciwgbik7CiAgICAgICAgIAogICAgY291dCA8PCAiXG5cblRoZSBudW1iZXIgb2YgZXhjaGFuZ2VzIG1hZGUgaW4gU2VsZWN0aW9uIFNvcnQgaXM6ICIgCiAgICAgICAgIDw8IHNlbGVjdGlvblNvcnQoYXJyMSwgbik7CgogICAgcmV0dXJuIDA7Cn0=