-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sorting.cpp
More file actions
33 lines (33 loc) · 829 Bytes
/
Copy pathbubble_sorting.cpp
File metadata and controls
33 lines (33 loc) · 829 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
#include <iostream>
using namespace std;
int main()
{
int array[] = {6, 5, 4, 1, 2, 3};
int n = sizeof(array) / sizeof(array[0]);
int numberOfSwap = 0;
int numberOfComp = 0;
for (int i = 0; i < n - 1; i++)
{
bool is_swap = false;
for (int j = 0; j < n - i - 1; j++)
{
numberOfComp++;
if (array[j + 1] < array[j])
{
numberOfSwap++;
is_swap = true;
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
if (!is_swap)
break;
}
cout << "number of swap :" << numberOfSwap << endl;
cout << "numer of comparison :" << numberOfComp << endl;
for (int i : array)
{
cout << i << " ";
}
}