S
SneakyElf
I have an array and a bubble sort, how to i keep track of the amount
of swaps that my program makes and displays it in the end?
here's my code so far. everything is done except for the count of
exchanges that are made:
#include <iostream>
using namespace std;
const int MAXSIZE = 20;
void bubbleSort ( int arr [ ], int size );
void swap ( int* x, int* y );
int main()
{
int numbers[MAXSIZE] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15, 1,
9, 78, 4, 22, 90, 18, 65, 89, 33};
int index;
cout << "BEFORE SORT: ";
for (index = 0; index < MAXSIZE; index++)
cout << numbers[index] << " ";
bubbleSort(numbers, MAXSIZE); // perform sort routine
cout << endl << endl << "AFTER SORT.: ";
for (index = 0; index < MAXSIZE; index++)
cout << numbers[index] << " ";
cout << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
// bubble sort technique defined for ascending order
void bubbleSort ( int arr [ ], int size )
{
int last = size - 2;
int isChanged = 1, k;
while ( last >= 0 && isChanged )
{
isChanged = 0;
for ( k = 0; k <= last; k++ )
if ( arr[k] > arr[k+1] )
{
swap ( &arr[k], &arr[k+1] );
isChanged = 1;
}
last--;
}
}
// interchanges values of two integer variables
void swap ( int* x, int* y )
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
of swaps that my program makes and displays it in the end?
here's my code so far. everything is done except for the count of
exchanges that are made:
#include <iostream>
using namespace std;
const int MAXSIZE = 20;
void bubbleSort ( int arr [ ], int size );
void swap ( int* x, int* y );
int main()
{
int numbers[MAXSIZE] = { 13, 5, 1, 7, 9, 11, 3, 17, 19, 15, 1,
9, 78, 4, 22, 90, 18, 65, 89, 33};
int index;
cout << "BEFORE SORT: ";
for (index = 0; index < MAXSIZE; index++)
cout << numbers[index] << " ";
bubbleSort(numbers, MAXSIZE); // perform sort routine
cout << endl << endl << "AFTER SORT.: ";
for (index = 0; index < MAXSIZE; index++)
cout << numbers[index] << " ";
cout << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
// bubble sort technique defined for ascending order
void bubbleSort ( int arr [ ], int size )
{
int last = size - 2;
int isChanged = 1, k;
while ( last >= 0 && isChanged )
{
isChanged = 0;
for ( k = 0; k <= last; k++ )
if ( arr[k] > arr[k+1] )
{
swap ( &arr[k], &arr[k+1] );
isChanged = 1;
}
last--;
}
}
// interchanges values of two integer variables
void swap ( int* x, int* y )
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}