forked from strang3-r/Leetcode75
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.cpp
More file actions
32 lines (30 loc) · 690 Bytes
/
Copy pathbubblesort.cpp
File metadata and controls
32 lines (30 loc) · 690 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
/* Here we just comapre the next element with the previous element and then do the same.....
n-i-1 becoz for each i the element will be sorted*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; i++)
{
bool swapped = false;
for (int j = 0; j < n - i - 1; j++)
{
if (a[j + 1] < a[j])
{
swapped = true;
swap(a[j + 1], a[j]);
}
}
if (!swapped)
{
break;
}
}
for (int i = 0; i < n; i++)
cout << a[i] << " ";
}