#include<iostream>
using namespace std;
void second(int *arr,int p,int r,int &fst,int &sec)
{
if(p<r)
{
int q = (p+r)/2;
int lfst,lsec,rfst,rsec;
second(arr,p,q,lfst,lsec);
second(arr,q+1,r,rfst,rsec);
if(lfst<rfst)
{
fst = lfst;
if(lsec = -1)
sec = rfst;
else
sec = rfst<lsec? rfst:lsec;
}
else
{
fst = rfst;
if(rsec = -1)
sec = lfst;
else
sec = lfst<rsec?lfst:rsec;
}
}
else
{
fst = arr[p];
sec = -1;
}
}
int main()
{
int arr[] = {3,6,8,3,7,9};
int fst,sec;
int len = sizeof(arr)/sizeof(arr[0]);
second(arr,0,len-1,fst,sec);
cout<<sec;
getchar();
return 0;
}
找第二小的数 ——分治思想
最新推荐文章于 2021-10-28 17:28:35 发布