跳到正文

个人记录

奇奇怪怪的排序

mhx_zzz约 8 分钟
原文
文章信息与版本

文章信息

发布
2024年4月21日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

Vecsort

原理:

移步普通平衡树的 vector 做法,用到查询第 kk 大和插入操作。

复杂度

遍历 O(n)O(n),插入 O(n)O(n),二分 O(nlogn)O(nlogn),总复杂度 O(n2+nlog(n))O(n^2+nlog(n)) (不知道为什么实际复杂度远小于 n2n^2。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define lc(p) (p<<1)
#define rc(p) (p<<1|1)
#define debug puts("-----------")
#define endl puts("")
#define re register
#define in inline
using namespace std;
const int N=1e6+7;

char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
in int read(){
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
   	return x*f;
}

int a[N],n;
vector<int>v;
in void vecsort(){
	for(re int i=1;i<=n;i++){
		v.insert(lower_bound(v.begin(),v.end(),a[i]),a[i]);
	}
	for(re int i=1;i<=n;i++){
		a[i]=v[i-1];
	}
}
signed main(){
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	vecsort();
	for(re int i=1;i<=n;i++){
		printf("%lld ",a[i]);
	}
	return 0;
}

seort

原理:

STL的 multisetmultiset,用了插入、遍历。

复杂度

遍历 O(n)O(n),插入 O(logn)O(logn),总复杂度 O(nlogn)O(nlogn)。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define lc(p) (p<<1)
#define rc(p) (p<<1|1)
#define debug puts("-----------")
#define endl puts("")
#define re register
#define in inline
using namespace std;
const int N=1e6+7;

char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
in int read(){
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
   	return x*f;
}

int a[N],n;
in void seort(){
	multiset<int>s;
	for(re int i=1;i<=n;i++){
		s.insert(a[i]);
	}
	int id=1;
	for(auto i:s){
		a[id++]=i;
	}
}
signed main(){
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	seort();
	for(re int i=1;i<=n;i++){
		printf("%lld ",a[i]);
	}
	return 0;
}

qsort

原理:

优先队列

复杂度

遍历 O(n)O(n),插入 O(logn)O(logn),总复杂度 O(nlogn)O(nlogn)

代码

C++
#include<bits/stdc++.h>
#define int long long
#define lc(p) (p<<1)
#define rc(p) (p<<1|1)
#define debug puts("-----------")
#define endl puts("")
#define re register
#define in inline
using namespace std;
const int N=1e6+7;

char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
in int read(){
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
   	return x*f;
}

int a[N],n;
in void qsort(){
	priority_queue<int>q;
	for(re int i=1;i<=n;i++){
		q.push(a[i]);
	}
	for(re int i=n;i>=1;i--){
		a[i]=q.top(),q.pop();
	}
}
signed main(){
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	qsort();
	for(re int i=1;i<=n;i++){
		printf("%lld ",a[i]);
	}
	return 0;
}

丐版快排

简陋到不能在简陋的排序,平时用来排排序挺好的。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define lc(p) (p<<1)
#define rc(p) (p<<1|1)
#define debug puts("-----------")
#define endl puts("")
#define re register
#define in inline
using namespace std;
const int N=1e6+7;

char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
in int read(){
    int x=0,f=1;
    char ch=nc();
    while(ch<48||ch>57){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
   	return x*f;
}

int a[N],n;
int siz;
int heap[N];
in void push(int x){
	int i=siz++;
	while(i>0){
		int p=(i-1)/2;
		if(heap[p]<=x) break;
		heap[i]=heap[p];
		i=p;
	}
	heap[i]=x;
}
in int top(){
	return heap[0];
}
in void pop(){
	int x=heap[--siz];
	heap[siz]=0;
	int i=0;
	while(i*2+1<siz){
		int a=i*2+1;
		int b=i*2+2;
		if(b<siz&&heap[b]<heap[a]){
			a=b;
		}
		if(heap[a]>=x) break;
		heap[i]=heap[a];
		i=a;
	}
	heap[i]=x;
}
in void qsort(){
	for(re int i=1;i<=n;i++){
		push(a[i]);
	}
	for(re int i=1;i<=n;i++){
		a[i]=top();
		pop();
	}
}
signed main(){
	n=read();
	for(re int i=1;i<=n;i++) a[i]=read();
	qsort();
	for(re int i=1;i<=n;i++){
		printf("%lld ",a[i]);
	}
	return 0;
}
原文