forked from Ansi007/Programming-Fundamentals-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort
48 lines (35 loc) · 784 Bytes
/
BubbleSort
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
#include <iostream>
using namespace std;
int main()
{
bool flag = 0;
int n;
cout << "Enter the size of the array" << endl;
cin >> n;
cout << endl;
int *arr = (int *)malloc(n * sizeof(int)); // float char types
cout << "Enter the elements of the array" << endl;
char check[1];
for (int i = 0; i <= n - 1; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
flag = 0;
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
flag = 1;
}
}
if (flag == 0)
break;
}
for (int i = 0; i <= n - 1; i++)
{
cout << arr[i] << " ";
}
}