-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.cpp
56 lines (48 loc) · 805 Bytes
/
bubble.cpp
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>
int bubble_sort(int *array, int n/*number of elements*/);
void swap(int &a, int &b);
int print_array(int *array, int n/*number of elements*/);
int main (int argc, char* argv[])
{
#define NUM 10
int a[NUM] = {1,4,2,6,3,5,3,2,6,6};
bubble_sort(a, NUM);
print_array(a, NUM);
return 0;
}
int bubble_sort(int *array, int n/*number of elements*/)
{
if (array == nullptr)
{
return 1;
}
if (n < 2)
{
return 0;
}
for (int i = n-1; i != 1; i--)
{
for (int j = 0; j < i; j++)
{
if (array[j] > array[i])
{
swap(array[j], array[i]);
}
}
}
}
void swap(int &a, int &b)
{
int c;
c = a;
a = b;
b = c;
}
int print_array(int *array, int n/*number of elements*/)
{
for (int i = 0; i < n; i++)
{
std::cout << array[i] << " ";
}
std::cout<<std::endl;
}