1.快速排序
#include<iostream>
#include<algorithm>
using namespace std;
int Median3(int A[],int Left,int Right)
{
int Center=(Left+Right)/2;
if(A[Left]>A[Center])
swap(A[Left],A[Center]);
if(A[Left]>A[Right])
swap(A[Left],A[Right]);
if(A[Center]>A[Right])
swap(A[Center],A[Right]);
swap(A[Center],A[Right-1]);
return A[Right-1];
}
void InsertionSort(int A[],int N)
{
int j,p;
int temp;
for(p=1;p<N;p++)
{
temp=A[p];
for(j=p;j>0&&A[j-1]>temp;j--)
A[j]=A[j-1];
A[j]=temp;
}
}
#define cutoff 3
void Qsort(int A[],int Left,int Right)
{
int i,j;
int pivot;
if(Left+cutoff<=Right)
{
pivot=Median3(A,Left,Right);
i=Left;j=Right-1;
for( ; ; )
{
while(A[++i]<pivot) {}
while(A[--j]>pivot) {}
if(i<j)
swap(A[i],A[j]);
else
break;
}
swap(A[i],A[Right-1]);
Qsort(A,Left,i-1);
Qsort(A,i+1,Right);
}
else
InsertionSort(A+Left,Right-Left+1);
}
void Quicksort(int A[],int N)
{
Qsort(A,0,N-1);
}
int main()
{
int n,*A,k;
cin>>n;
A=new int[n];
for(int i=0;i<n;i++)
cin>>A[i];
cin>>k;
Quicksort(A,n);
for(int i=n-1;i>=n-k;i--)
{
cout<<A[i]<<endl;
}
return 0;
}
2.快速排序的 partition 方法
#include<iostream>
#include<cstdio>
using namespace std;
int k;
int a[100005];
void swap(int & a,int & b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
void Qsort( int a[], int start, int end )
{
if(start >= end){
return;
}
int pivot = a[start];
int i = start,j = end;
while (i < j)
{
while ( j > i && a[j] >= pivot )
--j;
swap( a[i], a[j]);
while ( i < j && a[i] <= pivot )
++i;
swap( a[i], a[j]);
}
Qsort( a, start, i-1 );
Qsort( a, i+1, end );
}
void FindMaxK(int a[],int k, int start, int end)
{
if(start <= end)
{
int pivot = a[start];
int i = start, j = end;
while (i < j)
{
while ( j > i && a[j] >= pivot )
--j;
swap( a[i], a[j]);
while ( i < j && a[i] <= pivot )
++i;
swap( a[i], a[j]);
}
if(end - i + 1 == k){
return;
}
else if(end - i + 1 > k) {
FindMaxK(a,k,i+1,end);
}
else {
FindMaxK(a,k-(end-i+1),start,i-1);
}
}
}
int main()
{
int size;
scanf("%d",&size);
for(int i=0;i < size;++i){
scanf("%d",&a[i]);
}
scanf("%d",&k);
FindMaxK(a,k,0,size-1);
Qsort(a,size-k,size-1);
for(int i = size-1;i >= size - k;--i){
printf("%d\n",a[i]);
}
return 0;
}
3.最小堆
#include<iostream>
#include<cstdio>
using namespace std;
int k;
int a[100005];
void swap(int & a,int & b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
void PercDown( int A[], int p, int N )
{
int Parent, Child;
int X;
X = A[p]; /* 取出根结点存放的值 */
for( Parent=p; (Parent*2+1)<N; Parent = Child ) {
Child = Parent * 2 + 1;
if( ( Child != N-1 ) && ( A[Child] > A[Child+1] ) ){
Child++; /* Child指向左右子结点的较大者 */
}
if( X <= A[Child] ){
break; /* 找到了合适位置 */
}
else { /* 下滤X */
A[Parent] = A[Child];
}
}
A[Parent] = X;
}
void InsertionSort(int A[],int N)
{
int j,p;
int temp;
for(p=1;p<N;p++)
{
temp=A[p];
for(j=p;j>0&&A[j-1]>temp;j--)
A[j]=A[j-1];
A[j]=temp;
}
}
int main()
{
int size;
scanf("%d",&size);
for(int i=0;i < size;++i){
scanf("%d",&a[i]);
}
scanf("%d",&k);
int *arr ;
arr = new int[k];
for (int i = 0; i < k; ++i) {
arr[i] = a[i];
}
// 建最小堆
for (int var = size/2-1; var >= 0 ; --var) {
PercDown(arr,var,k);
}
// 检查剩余的size-k个数
for (int i = k; i < size; ++i) {
if(a[i] > arr[0]){
arr[0] = a[i];
PercDown(arr, 0 , k);
}
}
InsertionSort(arr,k);
for(int i = k-1;i >= 0;--i){
printf("%d\n",arr[i]);
}
return 0;
}