#include "iostream"
#include "string.h"
int partition(int a[],int left,int right)
{
int i = left;
int j = right;
int temp = a[i];
while (i<j)
{
while (i<j && a[j] >= temp)
j--;
if (i<j)
a[i] = a[j];
while (i<j && a[i] <= temp)
i++;
if (i<j)
a[j] = a[i];
}
a[i] = temp;
return i;
}
void quickSort(int a[],int left,int right)
{
int dp;
if (left < right)
{
dp = partition(a,left,right);
quickSort(a,left,dp-1);
quickSort(a, dp+1,right);
}
}
void topK(int a[],int left,int right,int k)
{
int dp;
if (left < right)
{
dp = partition(a,left,right);
if(dp<k)
topK(a,left,dp-1,k);
if(dp>k)
topK(a, dp+1,right,k);
}
}
int main()
{
int a[8]={3,4,1,6,4,8,5,1};
int b[8];
memcpy(b,a,sizeof(b));
//快排
quickSort(a,0,7);
for(int i=0;i<8;i++)
std::cout<<a[i]<<" ";
std::cout<<std::endl;
//topK
for(int k=1;k<=9;k++)
{
topK(b,0,8,k);
std::cout<<"第"<<k<<"大的数字是: "<<a[k-1]<<std::endl;
}
return 0;
}