Finding the algorithm to turn jobs

Source: Internet
Author: User

Preface:

Just wrote a vent article, sure enough, the heart is much more comfortable. Now that you're feeling comfortable, keep writing.

Assume:

We assume that the data being found is unique and there are no duplicate data in the array.

General Search:

Application Scenario:
Without features, it is necessary to traverse the entire range to be determined.

#include <iostream>#include <assert.h>//Common lookup algorithm. Template<unsignedN>intFind_array (Const int(&arr) [N],Const int& value) {//Use array type references and templates.     //Find successful return subscript, otherwise return -1.    if(arr = =nullptr|| n = =0)return-1; for(unsignedindex =0; Index < N;++index)if(Arr[index] = = value)returnIndexreturn-1;}//Implement as Class templateTemplate<unsignedNTypeNameT>intFind_narray (ConstT (&arr) [n],Constt& value) {if(arr = =nullptr|| n = =0)return-1; for(unsignedindex =0; Index < N;++index)if(Arr[index] = = value)returnIndexreturn-1;}intMain () {intarr[Ten] = {1,2,3,4,5}; ASSERT (Find_array (arr,Ten) == -1);intarr2[3] = {1,2,3}; ASSERT (Find_array (ARR2,2) ==1);Chararr3[3] ="HI"; ASSERT (Find_narray (ARR3,' I ')==1); System"Pause");return 0;}//Time complexity is O (n), find at least 1 times, and find up to n times. 
Two-point search:
#include <iostream>#include <functional>//Find successful return subscript. Template<unsignedLength>intBinary_find (Const int(&arr) [Length],Const int& value) {AutoBeg =0, end = Length-1; while(Beg <= end) {AutoMid = Beg + ((End-beg) >>1);//Use shift operator.         if(Arr[mid] = = value)returnMidElse if(Arr[mid] > value) End = mid-1;ElseBeg = mid +1; }return-1;}//Compare the following wordingTemplate<unsignedLength>intBinary_find_ (Const int(&arr) [Length],Const int& value) {AutoBeg =0, end = length;//The end=length here;     while(Beg < end) {//Contrast        intMid = Beg + ((End-beg) >>1);if(Arr[mid] = = value)returnMidElse if(arr[mid]<value) beg= mid+1;Elseend = Mid;//Contrast}return-1;}//RecursiveintBinary_find (int* p,intBegintEndConst int& value) {if(Beg < end) {AutoMid = Beg + ((End-beg) >>2);if(P[mid] = = value)returnMidElse if(P[mid]>value)returnBinary_find (P, Beg, Mid, value);Else            returnBinary_find (p, Mid +1, end, value); }return-1;}//Using a function template to achieveTemplate<unsignedLengthTypeNameT>intBinary_find (ConstT (&arr) [length],Constt& value) {AutoBeg =0, end = length; Equal_to<t> is_equal;//The callable object of the standard library. Greater<t> Is_greater; while(Beg<end) {AutoMid = Beg + ((End-beg) >>1);if(Is_equal (Arr[mid], value))returnMidElse if(Is_greater (Arr[mid], value)) end = mid;ElseBeg = mid +1; }return-1;}intMain () {intarr[5] = {1,2,3,4,5};//std::cout << binary_find (arr, 5)    STD::cout<<binary_find (arr,0,5, About); System"Pause");return 0;}
Summary:

All of a sudden, so tired, so leave next time to add it.
There are opportunities to complement recursive lookups in binary trees and lookup in hash tables and the use of lookup algorithms in STL.

Find the algorithm to turn on the job

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.