Display Bubble Sort

Program work but having problem with code that will prints the array at
each step of the algorithm.

#include <iostream>
using namespace std;

void bubbleSortArray(int [], int);
void displayArray(int[], int);
const int SIZE = 8;
int main()
{
int values[SIZE] = {20,0,45,-3,-78,1,-1,9};
cout << "The values before the bubble sort is performed are:" << endl;
displayArray(values,SIZE);
bubbleSortArray(values,SIZE);

cout << "The values after the bubble sort is performed are:" << endl;
displayArray(values,SIZE);
system ("pause");return 0;
}

void displayArray(int array[], int elems)
{

for (int count = 0; count < elems; count++)

cout << array[count] << " " << endl;


}

void bubbleSortArray(int array[], int elems)
{
bool swap;
int temp;
int bottom = elems - 1;


do
{
swap = false;
for (int count = 0; count < bottom;count++)
{
if (array[count] < array[count+1])
{
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true;
}
}

bottom--;
} while(swap != false );

}
use [ code ] [ / code ] next time without spaces, for readability.
also why would you bottom--; ? you're using it to remeber the amount of elements the array stores.
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
#include <iostream>
using namespace std;

void bubbleSortArray(int [], int);
void displayArray(int[], int);
const int SIZE = 8;
int main()
{
int values[SIZE] = {20,0,45,-3,-78,1,-1,9};
cout << "The values before the bubble sort is performed are:" << endl;
displayArray(values,SIZE);
bubbleSortArray(values,SIZE);

cout << "The values after the bubble sort is performed are:" << endl;
displayArray(values,SIZE);
system ("pause");return 0;
}

void displayArray(int array[], int elems)
{

for (int count = 0; count < elems; count++)

cout << array[count] << " " << endl;


}

void bubbleSortArray(int array[], int elems)
{
bool swap;
int temp;
int bottom = elems - 1;


do
{
swap = false;
for (int count = 0; count < bottom;count++)
{
if (array[count] < array[count+1])
{
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true;
}
}

bottom--;
} while(swap != false );

}
Last edited on
Topic archived. No new replies allowed.