1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intmaxn=10000;7 intF[MAXN];8 voidSwapint&a,int&B) {intt=a;a=b;b=t;}9 Ten /*********************************************/ One //all L are an array starting subscript, all r is an array end subscript A voidQuicksortintA[],intLintR//Quick Sort - { - if(l<R) the { - intt=a[l],i=l,j=R; - while(i!=j) - { + while(T<=a[j] && i<j) j--; - while(a[i]<=t && i<j) i++; + if(i<j) Swap (A[i],a[j]); A } ata[l]=a[i];a[i]=T; -Quicksort (a,l,i-1); -Quicksort (a,i+1, R); - } - } - in intBinary_search (intA[],intLintRintVal//binary lookup, not found return-1 - { to intmid,ans=-1; + while(l<=R) - { theMid= (l+r) >>1; * if(Val>a[mid]) l=mid+1; $ Else if(Val==a[mid])returnmid;Panax Notoginseng Elser=mid-1; - } the returnans; + } A the intLower_bound (intA[],intLintRintVal//binary Nether Lookup, no nether return-1 + { - intmid,ans=-1; $ while(l<=R) $ { -Mid= (l+r) >>1; - if(A[mid]<=val) ans=mid,l=mid+1; the Elser=mid-1; - }Wuyi returnans; the } - Wu intUpper_bound (intA[],intLintRintVal//Two-point last search, no last return-1 - { About intmid,ans=-1; $ while(l<=R) - { -Mid= (l+r) >>1; - if(A[mid]>=val) ans=mid,r=mid-1; A ElseL=mid+1; + } the returnans; - } $ /*********************************************/ the the intMain () the { the intI,n; - while(~SCANF ("%d",&N)) in { the for(i=1; i<=n;i++) scanf ("%d", f+i); theQuicksort (F,1, n); About for(i=1; i<=n;i++) printf ("%d", F[i]); thePuts""); the intt,p; thePuts"5-time binary search"); t=5; + while(t--) {scanf ("%d", &p);p rintf ("%d\n", Binary_search (F,1, N,p));} -Puts"5 sub-nether Search"); t=5; the while(t--) {scanf ("%d", &p);p rintf ("%d\n", Lower_bound (F,1, N,p));}BayiPuts"5 sub-upper bounds lookup"); t=5; the while(t--) {scanf ("%d", &p);p rintf ("%d\n", Upper_bound (F,1, N,p));} the } - return 0; -}
Quick Sort + Find