-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathbubble_sort.cpp
45 lines (40 loc) · 849 Bytes
/
bubble_sort.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
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "ENTER THE SIZE OF ARRAY: : ";
cin >> n;
int array[n];
cout << "ENTER THE ELEMENTS: " << endl;
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
cout << "UNSORTED ARRAY: " << endl;
for (int j = 0; j < n; j++)
{
cout << array[j] << " ";
}
cout << endl;
int temp;
for (int i = 0; i <= n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
cout << "SORTED ARRAY: " << endl;
for (int i = 0; i < n; i++)
{
cout << array[i] << " ";
}
cout << endl;
return 0;
}