1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iostream>
using namespace std;
void bubbleSortArray(int [], int);
void Sortbubble(int [], int);
const int SIZE = 5;
int main()
{
int values[SIZE] = {5, 4, 1, 2, 3};
int value[SIZE] = {5, 4, 1, 2, 3};
cout << "Sorting without the bool variable" << endl << endl;
Sortbubble(value, SIZE);
cout << "Sorting with the bool variable" << endl << endl;
bubbleSortArray(values, SIZE);
return 0;
}
void bubbleSortArray(int array[], int elems)
{
bool swap;
int temp, bottom = elems - 1;
int iter = 1;
do
{
swap = false;
cout << "No. " << iter << " iteration of the outer loop." << endl;
for (int count = 0; count < bottom; count++)
{
cout << "No. " << count + 1 << " iteration of the inner loop." << endl;
if (array[count] > array[count+1])
{
cout << "Swapping " << array[count] << " with " << array[count + 1] << endl;
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true;
}
}
bottom--;
iter++;
}
while(swap != false);
}
void Sortbubble(int array[], int elems)
{
int temp, bottom = elems - 1;
int iter = 1;
for (int j = 0; j < elems; j++)
{
cout << "No. " << iter << " iteration of the outer loop." << endl;
for (int count = 0; count < bottom; count++)
{
cout << "No. " << count + 1 << " iteration of the inner loop." << endl;
if (array[count] > array[count+1])
{
cout << "Swapping " << array[count] << " with " << array[count + 1] << endl;
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
}
}
iter++;
bottom--;
}
}
|