Data structure & Algorithm (two) _ Algorithm basis (recursion, time complexity, space complexity, binary search)

Source: Internet
Author: User

What is an algorithm:

Algorithm (algorithm): A computational process that solves the problem

Two characteristics of recursion:

    • Call itself
    • End condition
Recursive example:

def func (x):ifx==0: Print ("my little carp.", end="')    Else: Print ("hold the", end="') func (x-1) Print ("of Me", end="") func (5)
Recursive example one: my little carp
" "1 1 2 3 5 8  -  +  theFibonacci sequence with output length n" "#方式一: whilecycle def fibo (num): a=1b=1I=1     whilei<=Num:print (A,end=" ") A, b= b,a+b I+=1# Fibo (Ten) #方式二: Output A certain def fibo2 (num) with a recursive function #:ifnum = =1or num==2:        return 1elif Num>2:        returnFibo2 (num-1) +fibo2 (num-2) #s输出整个数列 # forIinchRange1, One): # Print (Fibo2 (i), End=" ") #方式三def Fibo3 (a,b,num):ifA >Num:returnPrint (A,end=" ") Fibo3 (B,a+b,num) Fibo3 (1,1,1100)
Recursive example two: Fibonacci sequenceComplexity of Time

Complexity of Space

Spatial complexity: An equation used to evaluate the size of an algorithm's memory footprint

With the spatial complexity of the program, you can have a pre-estimate of how much memory is needed to run the program. In addition to requiring storage space and storing the instructions, constants, variables, and input data used by the store itself, a program needs some working units to manipulate the data and a secondary space to store some of the information needed for realistic computing.  The storage space required for program execution consists of the following two parts. (1) Fixed part. The size of this part of the space is independent of the number of input/output data. It mainly includes space occupied by instruction space (i.e. code space), data space (constants, simple variables), etc. This part belongs to the static space. (2) Variable space, this part of the space mainly includes the dynamic allocation of space, as well as the space required for the recursive stack. The spatial size of this part is related to the algorithm. The storage space required for an algorithm is expressed in F (N). S (n) =o (f (n)) where n is the size of the problem, S (n) represents spatial complexity. Two-point Search

Ideas:

Starting from the candidate area of the ordered list Data[0:n], the candidate area can be halved by comparing the value of the lookup with the median of the candidate area.

Characteristics:
Binary search for ordered list time complexity O (LOGN)
#!/usr/bin/env python#-*-coding:utf-8-*-' binary search for ordered list time complexity O (LOGN) ' Def Bin_search (li,val): low = 0 high = Len (LI)-1 while Li[low] <= Li[high]: Mid = (Low+high)//2 if Li[mid] = = Val:print ("Search success!            The index is:{0} ". Format (mid)) return None elif Li[mid] < Val:low = Mid+1 Else: High = Mid-1 Else:print ("Val was not exist") return none# binary lookup recursive version: Def bin_search_rec (Li,val,lo W,high): Mid = (Low+high)//2 while Li[low] <= Li[high]: if li[mid] = = Val:print ("Search succes s! The index is:{0} ". Format (mid)) return None elif Li[mid] < Val:return Bin_search_rec (Li, V Al, Mid+1, High) Else:return Bin_search_rec (Li, Val, Low, mid-1) Else:print ("Val are not Exis T ") return Noneli = [1,3,6,8,9,11,14,16,22,31,44,56,58]bin_search_rec (li,23,0,12) 

  

Data structure & Algorithm (two) _ Algorithm basis (recursion, time complexity, space complexity, binary search)

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.