Quick Sort + Find

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intmaxn=10000;7 intF[MAXN];8 voidSwapint&a,int&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.