- 题目地址
- 题目分析:都在20+ms左右,如果有更快的方法,欢迎分享
/*快排*/
#include<stdio.h>
#define SWAP(x,y) {a[0]=x;x=y;y=a[0];}//be carefull ; is exists
int a[100001];
void Qsort(int low,int high);
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);
Qsort(1,n);
for (int i = 1; i <= n; i++)
printf("%d%c",a[i],i==n?'\n':' ');
return 0;
}
void Qsort(int low,int high)
{
int tol=high-low+1;//待排总数
if(tol<2) return;
int mid=(low+high)/2;
if(a[low]>a[mid]) SWAP(a[low],a[mid])
if(a[low]>a[high]) SWAP(a[low],a[high])
if(a[mid]>a[high]) SWAP(a[mid],a[high])
if(tol>3)
{
int left=low,right=high-1,pivot=high-1;
SWAP(a[mid],a[pivot])
while (1)
{
while (a[++left]<a[pivot]);
while (a[--right]>a[pivot]);
if(left<right) SWAP(a[left],a[right])
else break;
}
SWAP(a[left],a[pivot])
Qsort(low,left-1);
Qsort(left+1,high);
}
}
/*快排的非递归形式*/
#include<stdio.h>
#define SWAP(x,y) {a[0]=x;x=y;y=a[0];}//be carefull ; is exists
int a[100001],stack[200001],top=-1;
void Qsort(int low,int high);
void BuildHeap(int heap[],int tol);
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);
Qsort(1,n);
for (int i = 1; i <= n; i++)
printf("%d%c",a[i],i==n?'\n':' ');
return 0;
}
void Qsort(int low,int high)
{
stack[++top]=low,stack[++top]=high;
while (top!=-1)
{
int R=stack[top--],L=stack[top--];
int tol=R-L+1;
if(tol<2) continue;
int mid=(L+R)/2;
if(a[L]>a[mid]) SWAP(a[L],a[mid])
if(a[L]>a[R]) SWAP(a[L],a[R])
if(a[mid]>a[R]) SWAP(a[mid],a[R])
if(tol>3)
{
int left=L,right=R-1,pivot=R-1;
SWAP(a[mid],a[pivot])
while (1)
{
while (a[++left]<a[pivot]);
while (a[--right]>a[pivot]);
if(left<right) SWAP(a[left],a[right])
else break;
}
SWAP(a[left],a[pivot])
stack[++top]=L,stack[++top]=left-1;
stack[++top]=left+1,stack[++top]=R;
}
}
}
/*快排+简单插入*/
#include<stdio.h>
#define SWAP(x,y) {a[0]=x;x=y;y=a[0];}//be carefull ; is exists
int a[100001];
void Qsort(int low,int high);
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);
Qsort(1,n);
for (int i = 1; i <= n; i++)
printf("%d%c",a[i],i==n?'\n':' ');
return 0;
}
void Qsort(int low,int high)
{
if(high-low>6)
{
int mid=(low+high)/2;
if(a[low]>a[mid]) SWAP(a[low],a[mid])
if(a[low]>a[high]) SWAP(a[low],a[high])
if(a[mid]>a[high]) SWAP(a[mid],a[high])
int left=low,right=high-1,pivot=high-1;
SWAP(a[mid],a[pivot])
while (1)
{
while (a[++left]<a[pivot]);
while (a[--right]>a[pivot]);
if(left<right) SWAP(a[left],a[right])
else break;
}
SWAP(a[left],a[pivot])
Qsort(low,left-1);
Qsort(left+1,high);
}
else
{
for (int i = low+1,tmp,j; i <= high; i++)
{
tmp=a[i];
for(j=i;j>low&&tmp<a[j-1];j--)
a[j]=a[j-1];
a[j]=tmp;
}
}
}
/*快排+堆排序*/
#include<stdio.h>
#define SWAP(x,y) {A[0]=x;x=y;y=A[0];}//be carefull ; is exists
int A[100001];
void Qsort(int low,int high);
void buildHeap(int H[],int tol);
void percDown(int H[],int tol,int k);
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++)
scanf("%d",&A[i]);
Qsort(1,n);
for (int i = 1; i <= n; i++)
printf("%d%c",A[i],i==n?'\n':' ');
return 0;
}
void Qsort(int low,int high)
{
if(high-low>70)
{
int mid=(low+high)/2;
if(A[low]>A[mid]) SWAP(A[low],A[mid])
if(A[low]>A[high]) SWAP(A[low],A[high])
if(A[mid]>A[high]) SWAP(A[mid],A[high])
int left=low,right=high-1,pivot=high-1;
SWAP(A[mid],A[pivot])
while (1)
{
while (A[++left]<A[pivot]);
while (A[--right]>A[pivot]);
if(left<right) SWAP(A[left],A[right])
else break;
}
SWAP(A[left],A[pivot])
Qsort(low,left-1);
Qsort(left+1,high);
}
else
{
buildHeap(A+low-1,high-low+1);//H is 1 to tol, dont have 0
for (int i = high; i > low; i--)
{
SWAP(A[low],A[i]);
percDown(A+low-1,i-low,1);
}
}
}
void buildHeap(int H[],int tol)
{
for(int i=tol/2;i>0;i--)
percDown(H,tol,i);
}
void percDown(int H[],int tol,int k)
{
int tmp=H[k],i;
for (i=2*k;i<=tol;i*=2)
{
if(i+1<=tol&&H[i]<H[i+1]) i++;
if(tmp<H[i]) H[i/2]=H[i];
else break;
}
H[i/2]=tmp;
}