-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.cpp
More file actions
56 lines (43 loc) · 937 Bytes
/
bubble.cpp
File metadata and controls
56 lines (43 loc) · 937 Bytes
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
#include <iostream>
using namespace std;
int bubbleSort(int [], int);
void showBubble(const int [], int);
int counter = 0;
int main()
{
const int SIZE = 10;
int values[SIZE] = {2, 5, 7, 9, 1, 3, 4, 6, 8, 10};
cout << "The unsorted values are:\n";
showBubble(values, SIZE);
bubbleSort(values, SIZE);
cout << "The sorted values are:\n";
showBubble(values, SIZE);
cout << "The loop ran " << counter << " times.";
return 0;
}
int bubbleSort(int array[], int size)
{
int temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
counter++;
}
}
} while (swap);
return counter;
}
void showBubble(const int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}