recursive function and binary search algorithm

Source: Internet
Author: User

Recursive functions

definition: call the function itself in a function

Maximum recursion Depth: 997

To modify the maximum depth: Import SYS Print (Sys.setrecursionlimit (100000))
# Recursive example-seeking age def Age (N):     if n = =1        :return    , Else:        return age (n-1) +2Print(age (4))
Binary search algorithm Simple version dichotomy method
L = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]deffunc (L,aim): Mid= (Len (l)-1)//2ifL:ifAim >L[mid]: func (L[mid+1:],aim)elifAim <L[mid]: func (L[:mid],aim)elifAim = =L[mid]:Print("Bingo", mid)Else:        Print('I can't find them .') func (L,66) func (L,6) The basic version of the dichotomy method
two-way version of the upgrade
defSearch (num,l,start=none,end=None): Start= StartifStartElse0 End= EndifEnd isNoneElseLen (L)-1Mid= (End-start)//2 +StartifStart >End:returnNoneelifL[mid] >Num:returnSearch (num,l,start,mid-1)    elifL[mid] <Num:returnSearch (num,l,mid+1, end)elifL[mid] = =Num:returnMid binary Algorithm Ultimate Edition

recursive function and binary 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.