归并排序
模版
#include<iostream>
#include<cstdio>
using namespace std;
int n,a[100100],b[100100];
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool mode)
{
x<0?x=-x,putchar('-'):0;static short Stack[100],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
mode?puts(""):putchar(' ');
}
void merge_sort(int l,int r)
{
if(l==r) return;
int mid=(l+r)>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int t1=l,t2=mid+1;
for(int i=l;i<=r;i++)
{
if(t1>mid) b[i]=a[t2++];
else if(t2>r) b[i]=a[t1++];
else if(a[t1]<=a[t2]) b[i]=a[t1++];//若要从大到小排序,只需将这里的<=改为>即可
else b[i]=a[t2++];
}
for(int i=l;i<=r;i++) a[i]=b[i];
return;
}
signed main()
{
read(n);
for(int i=1;i<=n;i++) read(a[i]);
merge_sort(1,n);
for(int i=1;i<=n;i++) write(a[i],0);
return 0;
}
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll a[50000100],n,ans,b[50000100];
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool mode)
{
x<0?x=-x,putchar('-'):0;static short Stack[100],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
mode?puts(""):putchar(' ');
}
ll merge_sort(int l,int r)
{
if(l==r) return 0;
int m=(l+r)>>1;
ll res=merge_sort(l,m)+merge_sort(m+1,r);
int t1=l,t2=m+1;
for(int i(l);i<=r;++i)
{
if(t1>m) b[i]=a[t2++];
else if(t2>r) b[i]=a[t1++];
else if(a[t1]<=a[t2]) b[i]=a[t1++];
else b[i]=a[t2++],res+=m-t1+1;
}
for(int i(l);i<=r;++i) a[i]=b[i];
return res;
}
signed main()
{
read(n);
for(int i(1);i<=n;++i) read(a[i]);
// for(int i=1;i<=n;i++) write(a[i],0);
write(merge_sort(1,n),1);
return 0;
}
堆排序
模板
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
using namespace std;
namespace ly
{
auto swap=[](auto &a,auto &b){auto t=a;a=b;b=t;};
}
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool mode)
{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
mode?putchar('\n'):putchar(' ');
}
class heap
{
public:
heap(){type=0;n=0;memset(a,0,sizeof(a));}
//0:min_heap 1:max_heap
heap(bool mode){type=mode;}
void up(int i)
{
if(type) while(i^1&&a[i]>a[i>>1]) ly::swap(a[i],a[i>>1]),i>>=1;
else while(i^1&&a[i]<a[i>>1]) ly::swap(a[i],a[i>>1]),i>>=1;
}
void down(int i)
{
i<<=1;
if(type)
while(i<=n)
{
if(i<n&&a[i+1]>a[i]) i++;
if(a[i]>a[i>>1]) ly::swap(a[i],a[i>>1]),i<<=1;
else break;
}
else
while(i<=n)
{
if(i<n&&a[i+1]<a[i]) i++;
if(a[i]<a[i>>1]) ly::swap(a[i],a[i>>1]),i<<=1;
else break;
}
}
void push(int x)
{
a[++n]=x;
up(n);
}
void pop()
{
ly::swap(a[1],a[n--]);
down(1);
}
void erase(int i)
{
ly::swap(a[i],a[n--]);
up(i),down(i);
}
int top(){return a[1];}
int size(){return n;}
bool empty(){return !n;}
int visit(int i){return a[i];}
void traversal()
{
int t[maxn],cnt=0;
while(n) t[++cnt]=a[1],pop();
n=cnt;
for(int i=1;i<=n;i++) a[i]=t[i],write(t[i],0);
puts("");
}
private:
int type;
int n;
int a[maxn];
};
signed main()
{
heap h(0);
int n,x;
read(n);
for(int i=1;i<=n;i++) read(x),h.push(x);
h.traversal();
return 0;
}