Algorithm plots (binary search)

Source: Internet
Author: User

Objective:

Today is the first time to write every day to read the details of the timing of the current while learning Python, Java, go and algorithms so write less

It's mostly python, like, 16 hours a day. Java, go to see the timing of this algorithm is full of Python demonstrations do not understand Python does not matter

Or you can take a look at my previous post

What the algorithm is:

An algorithm is a group of instructions to complete a task either fast or solves the problem

The code of logic and thought that should be said to achieve some purpose

Two-point Search

Let's say you're looking for someone in the phone book K that might slide directly into the middle because you know K is in the middle.

Because the phone book is abcdef sort of.

Defined:

Binary search means a logical approach to finding from the middle

Note:

# binary lookup must be an ordered sequence to return its position

# unordered cannot make binary lookup returns null

For example:

Two-point search:

Using a binary lookup to take an intermediate number each time a decimal can be taken up or down the integer

The number within 100 can be found up to 7 times.

General Search:

While normal lookups are the best way to traverse from the ground up 11 times to find out

Worst is 100 times.

Binary Lookup with the addition of the element does not change too much

Normal lookups increase with the addition of elements

For example, there are 240,000 words in a dictionary.

Normal find worst case: 240,000 results

Two-point lookup worst case: 17 results

This clearly highlights the advantages of the two-point algorithm

# # # #这里我用 () The numbers in parentheses represent the subscript of the log

Log (2) n steps are required for two-point lookups

is actually using logarithmic operations:

Logarithmic operations:

Definition: Inverse of a power operation

For example:

10**2 = log (10) 100 = 2

10**3 = log (10) 1000=3

2**5 = + log (2) 32 = 5

If there are 8 elements you need to find up to 3 times because LONG8 = 3 (2**3=8)

1024 elements require a maximum of 10 elements to be checked because 1024 = 10 (2**10=1024)

defbinary (LST, item): Low=0 High= Len (LST)-1 whileLow <=High:mid= Round (low + high)/2) Guess=Lst[mid]ifGuess = =item: # Guess it rightreturnMidifGuess >Item:high= mid-1 # big guess.Else: Low= mid + 1 # guess it's smallreturnnonemy_list= [1, 3, 5, 7, 9]Print(Binary (My_list, 3) # 1 The returned element subscript index is 0 startPrint(Binary (My_list,-1)) # None because there is no-1 element

Run Time:

Linear duration (linear time)

100 numbers guess up to 100 times 4 billion guess 4 billion times the maximum number of guesses equals the length of the list

Logarithmic time (or log time)

100 numbers guess up to 7 times 4 billion guess 32 times

Large O notation:

Simple lookup of each element requires n run time O (n)

Binary lookup run time is O (log (n))

Large o notation calculates the operand

O (log n) log time includes binary search

O (n) Linear time

O (n * log n) fast sorting algorithm

O (n**2) Speed-slower sequencing algorithm

O (n! ) Very slow algorithm

Summary:

Binary lookup is much faster than simple lookup

O (log n) is faster than O (n) and the more elements that need to be searched, the more quickly the former is faster than the latter

Algorithm run time is not measured in seconds

The algorithm run time is measured from the angle of its growth rate

The running time of the algorithm is represented by the large O notation

Algorithm plots (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.