排序合集
https://www.luogu.com.cn/problem/P1177
排序
快速排序(\(QuickSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,a[N];
template<typename T>
void QuickSort(T *a,int l,int r)
{
int i=l,j=r,k=(l+r) >> 1;
T t=a[k];
a[k]=a[i];
while (i<j)
{
while (i<j && a[j]>t)
j--;
if (i<j)
swap(a[i],a[j]),i++;
while (i<j && a[i]<t)
i++;
if (i<j)
swap(a[i],a[j]),j--;
}
a[i]=t;
if (l<i)
QuickSort(a,l,i-1);
if (i<r)
QuickSort(a,i+1,r);
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
QuickSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
随机化快排(\(Random\_QuickSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,a[N];
template<typename T>
void Random_QuickSort(T *a,int l,int r)
{
int i=l,j=r,k=rand()%(r-l+1)+l;
T t=a[k];
a[k]=a[i];
while (i<j)
{
while (i<j && a[j]>t)
j--;
if (i<j)
swap(a[i],a[j]),i++;
while (i<j && a[i]<t)
i++;
if (i<j)
swap(a[i],a[j]),j--;
}
a[i]=t;
if (l<i)
Random_QuickSort(a,l,i-1);
if (i<r)
Random_QuickSort(a,i+1,r);
}
int main()
{
srand(time(NULL));
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
Random_QuickSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
希尔排序(\(ShellSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,a[N];
template<typename T>
void ShellSort(T *a,int l,int r)
{
int len=r-l+1;
for (int gap=(len >> 1);gap;gap >>=1)
for (int i=gap+1;i<=len;i++)
for (int j=i-gap;j>0 && a[l+j-1]>a[l+j+gap-1];j-=gap)
swap(a[l+j-1],a[l+j+gap-1]);
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
ShellSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
归并排序(\(MergeSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
using namespace std;
int n,a[N],c[N];
template<typename T>
void MergeSort(T *a,int l,int r)
{
if (l==r)
return;
int mid=(l+r) >> 1;
MergeSort(a,l,mid);
MergeSort(a,mid+1,r);
int i=l,j=mid+1,k=l;
while (i<=mid || j<=r)
if (j>r || i<=mid && a[i]<=a[j])
c[k++]=a[i],i++; else
c[k++]=a[j],j++;
memcpy(a+l,c+l,(r-l+1)*sizeof(T));
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
MergeSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
基数排序(\(RadixSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
using namespace std;
#define p 256
int n,a[N],ot[N],t[p];
template<typename T>
void RadixSort(T *a,int l,int r)
{
T mx=a[l];
for (int i=l+1;i<=r;i++)
if (a[i]>mx)
mx=a[i];
int ws=0;
while (mx)
{
ws++;
mx/=p;
}
for (int i=1,g=1;i<=ws;i++)
{
memset(t,0,sizeof(t));
for (int j=l;j<=r;j++)
t[(a[j] / g) % p]++;
for (int j=1;j<p;j++)
t[j]+=t[j-1];
for (int j=r;j>=l;j--)
ot[t[(a[j] / g) % p]--]=a[j];
memcpy(a+l,ot+1,(r-l+1)*sizeof(T));
g*=p;
}
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
RadixSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
计数排序(\(CountSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
#define M 10000005
using namespace std;
int n,a[N],c[M];
template<typename T>
void CountSort(T *a,int l,int r)
{
T mn=a[l],mx=a[l];
for (int i=l+1;i<=r;i++)
mn=(a[i]<mn)?a[i]:mn,mx=(a[i]>mx)?a[i]:mx;
memset(c+mn+1,0,(mx-mn+1)*sizeof(T));
for (int i=l;i<=r;i++)
c[a[i]]++;
int g=l;
for (int i=mn;i<=mx;i++)
for (int j=1;j<=c[i];j++)
a[g++]=i;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
CountSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
借助\(map\)实现的计数排序(\(MapCountSort\))
注意\(MapCountSort\)中的数据类型可能需要修改
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#define N 100005
using namespace std;
int n,a[N];
map<int,int>c;
template<typename T>
void MapCountSort(T *a,int l,int r)
{
c.clear();
for (int i=l;i<=r;i++)
c[a[i]]++;
int g=l;
for(map<int,int>::iterator it=c.begin();it!=c.end();it++)
for (int i=1;i<=(*it).second;i++)
a[g++]=(*it).first;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
MapCountSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
堆排序(\(HeapSort\))
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#define N 100005
using namespace std;
int n,a[N];
priority_queue<int>q;
template<typename T>
void HeapSort(T *a,int l,int r)
{
while (!q.empty())
q.pop();
for (int i=l;i<=r;i++)
q.push(a[i]);
for (int i=r;i>=l;i--)
a[i]=q.top(),q.pop();
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
HeapSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
基于\(multiset\)的排序(\(MultisetSort\))
注意\(MultisetSort\)中的数据类型可能需要修改
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#define N 100005
using namespace std;
int n,a[N];
multiset<int>s;
template<typename T>
void MultisetSort(T *a,int l,int r)
{
s.clear();
for (int i=l;i<=r;i++)
s.insert(a[i]);
for (multiset<int> :: iterator it=s.begin();it!=s.end();++it)
a[l++]=*it;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
MultisetSort(a,1,n);
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}