-
Notifications
You must be signed in to change notification settings - Fork 1
/
BUBBLESORT.cpp
92 lines (75 loc) · 1.45 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
88
89
90
91
92
// BUBBLESORT - Bubble Sort
// Author: Tarun Kumar
// E-mail: [email protected]
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define fab(i,a,b) for(i=a;i<=b;i++)
#define display(x) for(auto dsp:x)cout<<dsp<<" ";cout<<"\n";
#define hi cout<<"hi\n";
#define ln "\n"
using namespace std;
ll mod=10000007;
ll cnt;
void merge(ll a[],ll l,ll md,ll r)
{
ll tmp[r-l+1];
ll i=l,j=md+1,k=0;
while(i<=md&&j<=r)
{
if(a[i]<=a[j])
{
tmp[k++]=a[i];
i++;
}
else
{
tmp[k++]=a[j];
j++;
cnt+=(md-i+1);
cnt%=mod;
}
}
while(i<=md)
{
tmp[k++]=a[i];
i++;
}
while(j<=r)
{
tmp[k++]=a[j];
j++;
}
fab(i,l,r)
a[i]=tmp[i-l];
}
void merge_sort(ll a[],ll l,ll r)
{
if(l<r)
{
ll md=(l+r)/2;
merge_sort(a,l,md);
merge_sort(a,md+1,r);
merge(a,l,md,r);
}
}
int main()
{
FAST;
ll t,n,i,j,k,len,x,y,z,c,f,flag,p,q,mx,mn,l,r,sum,ans,tmp,it,pos,avg,m;
cin>>t;
it=0;
while(t--)
{
it++;
cout<<"Case "<<it<<": ";
cin>>n;
ll a[n];
fab(i,0,n-1)
cin>>a[i];
cnt=0;
merge_sort(a,0,n-1);
cout<<cnt%mod<<ln;
}
return 0;
}