快排之一

//递归实现快排

#include
<iostream>
using namespace std;
int arr[1000];
int part(int beg, int end)
{
int low=beg,high=end;
int pivot=arr[beg];
while(low<high)
{
while(low<high && arr[high]>=pivot)
--high;
arr[low]
=arr[high];
while(low<high && arr[low]<=pivot)
++low;
arr[high]
=arr[low];
}
arr[low]
=pivot; //low=high
return low;
}
void QuickSort(int beg,int end)
{
if(beg<end)
{
int p=part(beg,end);
QuickSort(beg,p
-1);
QuickSort(p
+1,end);
}
}
int main()
{
int n;
cin
>>n;
for(int i=0; i<n; ++i)
cin
>>arr[i];
QuickSort(
0,n-1);
for(int i=0;i<n;++i)
cout
<<arr[i]<<" ";
cout
<<endl;
return 0;
}

  

//poj 2104 K-th Number


#include
<iostream>
using namespace std;
struct node
{
int index;
int num;
}list[
100000];
int part(int beg,int end)
{
int low=beg,high=end;
node pivot
=list[beg];
while(low < high)
{
while(low<high && list[high].num>=pivot.num)
--high;
list[low].index
=list[high].index;
list[low].num
=list[high].num;
while(low<high && list[low].num<=pivot.num)
++low;
list[high].index
=list[low].index;
list[high].num
=list[low].num;
}
list[low].index
=pivot.index;
list[low].num
=pivot.num;
return low;
}
void QuickSort(int beg,int end)
{
if(beg<end)
{
int p=part(beg,end);
QuickSort(beg,p
-1);
QuickSort(p
+1,end);
}
}
int main()
{
int n,m,t,i,j,k;
scanf(
"%d%d",&n,&m);
for(t=0;t<n;++t)
{
list[t].index
=t+1;
scanf(
"%d",&list[t].num);
}
QuickSort(
0,n-1);
while(m--)
{
scanf(
"%d%d%d",&i,&j,&k);
for(t=0;t<n;++t)
{
if(list[t].index>=i&&list[t].index<=j)
k
--;
if(k==0)
break;
}
printf(
"%d\n",list[t].num);
}
return 0;
}

  

posted on 2011-08-22 15:08  sysu_mjc  阅读(166)  评论(0编辑  收藏  举报

导航