Introduction and implementation of search algorithm

Source: Internet
Author: User

1 //Find algorithm:2 //Sequential Lookup3 //binary lookup (interpolation lookup, Fibonacci lookup)4 //Two fork sort Tree lookup5 //Hash Table Lookup6 7#include <iostream>8 using namespacestd;9 Ten  One /*Sequential Lookup*/ A intSequencesearch (int* Arr,intLenintN) - { -      for(inti =0; i < Len; i++) the         if(Arr[i] = =N) -             returni; -  -     return-1; + } -  + /*Binary Lookup (non-recursive version)*/ A intBinarySearch (int* Arr,intLenintN) at { -     intLow =0, high = n-1, mid; -      while(Low <=High ) -     { -Mid = (low + high) >>1; -         if(Arr[mid] = =N) in             returnmid; -         Else if(Arr[mid] <N) toLow = mid +1; +         Else -High = mid-1; the     } *  $     return-1;Panax Notoginseng } -  the /*Binary Lookup (recursive version)*/ + intBinarySearch0 (int* Arr,intNintLowintHigh ) A { the     if(Low <=High ) +     { -         intMid = (low + high) >>1; $  $         if(Arr[mid] = =N) -             returnmid; -         Else if(Arr[mid] <N) theBinarySearch0 (arr, N, Mid +1, high); -         ElseWuyiBinarySearch0 (arr, N, Low, mid-1); the     } -  Wu     return-1; - } About  $ /*Interpolation Lookup*/ - intInsertsearch (int* Arr,intLenintN) - { -     intLow =0, high = Len-1, mid; A      while(Low <=High ) +     { the         if(Arr[low] = = Arr[high])//handling cases with the same value in an array -             if(Arr[low] = =N) $                 returnLow ; the             Else the                 return-1; the  theMID = (int)(1.0* (N-arr[low])/(Arr[high]-arr[low]) * (high-low));//need to convert to double for calculation -         if(Arr[mid] = =N) in             returnmid; the         Else if(Arr[mid] <N) theLow = mid +1; About         Else theHigh = mid-1; the     } the  +     return-1; - } the Bayi  the  the intMain () - { -cout <<"Please enter an array sequence and a lookup element \ n (format: sequence length n element 1 element 2 ...) element n with Find element num):"<<Endl; the     intN, *Parr, Num; theCIN >>N; theParr =New int[n]; the      for(inti =0; I < n; i++) -CIN >>Parr[i]; theCIN >>num; the  thecout <<"\n1 Order Lookup, 22-point lookup (non-recursive), 32-point lookup (Recursive), 4 interpolation lookup; \ n";94cout <<"Please select a lookup mode:"; the     intselection, index; the      while(Cin >>selection) the     {98         Switch(selection) About         { -          Case 1:101index =Sequencesearch (Parr, N, num);102              Break;103          Case 2:104index =BinarySearch (Parr, N, num); the              Break;106          Case 3:107index = binarySearch0 (Parr, NUM,0N1);108              Break;109          Case 4: theindex =Insertsearch (Parr, N, num);111              Break; the         default:113              Break; the         } the  the         if(Index = =-1)117cout <<"Find failed! "<<Endl;118         Else119cout <<"Find success! Location:"<< Index +1<<Endl; - 121cout <<"\ n Please select a lookup mode:";122     }123 124     return 0; the}

Introduction and implementation of search algorithm

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.