-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.cpp
87 lines (81 loc) · 1.42 KB
/
BubbleSort.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<unordered_set>
#include<stack>
#include<iostream>
#include<vector>
#include<string>
#include<numeric>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<set>
#include<climits>
#include<cmath>
#include<math.h>
#include<limits>
#define ll long long int
const ll N = 1e5+ 2,MOD = 1e9+7;
using namespace std;
int power(ll a,ll b) {
ll ans = 1;
while(b>0){
if(b%2){
ans=ans*a;
}
a=a*a;b>>=1;
}
return ans;
}
ll countSetBits(ll n) {
ll count = 0;
while (n)
{
count += n & 1;
n >>= 1;
}
return count;
}
ll gcd(ll a, string b){
ll res = 0;
for (int i = 0; i < b.length(); i++){
res = ((res * 10) + (b[i] - '0')) % a;
}
return __gcd(a,res);
}
void BubbleSort(std::vector<ll> &v,ll n){
for(ll i=1;i<n;i++){
bool swaped = false;
for(ll j=0;j<n-1;j++){
if(v[j+1] > v[j]){
swap(v[j+1],v[j]);
swaped = true;
}
}
if(!swaped) break;
}
for(ll i=0;i<n;i++){
cout<<v[i]<<" ";
}cout<<endl;
}
void solve(){
ll n;cin>>n;
vector<ll> v(n);
for(ll i=0;i<n;i++){
cin>>v[i];
}
BubbleSort(v,n);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}