The basic search details of data structure and algorithm implemented by Python _python

Source: Internet
Author: User

This paper illustrates the basic search of data structure and algorithm implemented by Python. Share to everyone for your reference. The specific analysis is as follows:

First, sequential search

Sequential Search is the simplest and most intuitive way to search for items from the beginning of the list to the end, one at a time, and the items in the list, until you find the target item (the search succeeds) or out of search (the search fails).

You can divide the list into unordered lists and ordered lists, depending on whether the items in the list are sorted sequentially. For unordered lists, exceeding the search scope means crossing the end of the list, and for the ordered list, exceeding the search scope is the area that enters the list that is greater than the target item (occurs when the target item is less than the end of the list) or crosses the end of the list (when the target item is greater than the end of the

1. Unordered list

The sequential search in the unordered list is as shown in the figure:

def sequentialsearch (items, target): For
  item in items:
    if item = =: Return
      True
  

2. Ordered list

A sequential search in a sequence table is shown in the following illustration:

def orderedsequentialsearch (items, target): For the
  item in items:
    if item = =: Return
      True
    elif Item > Target: Break return
  False

Two or two-minute search

In fact, the above Orderedsequentialsearch algorithm does not make good use of the characteristics of the sequence table.

The binary search makes full use of the advantage of an ordered list, and the algorithm is very ingenious: in the original list, the target item (targets) is compared to the median of the list (middle), and if Target equals middle, the search succeeds If Target is less than middle, the search continues in the left half of the middle list, and if Target is greater than middle, the search continues in the right half of the middle list.

A binary search is performed in a sequence table as shown in the figure:

According to the different implementation methods, the binary search algorithm can be divided into iteration version and recursive version of two kinds:

1, Iteration version

def iterativebinarysearch (items, target):
  0 Last
  = Len (items)-1 while the
  <= last:
    middle = (i + last)//2
    if target = Items[middle]: return
      True
    elif Target < Items[middle]: last
      = Midd Le-1
    Else:
      Middle + 1 return
  False

2, recursive version

def recursivebinarysearch (items, target):
  If len (items) = = 0: return
    False
  else:
    middle = len (items)// 2
    If target = = Items[middle]: return
      True
    elif Target < Items[middle]: return
      Recursivebinarysearch (Items[:middle], target)
    else: Return
      Recursivebinarysearch (items[middle+1:], target)

Third, performance comparison

The time complexity of the above search algorithm is as follows:

Search algorithm          time complexity
-----------------------------------
sequentialsearch      O (n)
---------------- -------------------
orderedsequentialsearch  O (n) 
-----------------------------------
Iterativebinarysearch   O (log n)
-----------------------------------
recursivebinarysearch   O ( Log n)
-----------------------------------
in             O (n)

As you can see, the performance of binary search is better than sequential search .

It is worth noting that Python's member operators in the time complexity is O (n), it is not difficult to guess, operator in the actual use of sequential search algorithm.

Four, algorithm test

#!/usr/bin/env python
#-*-coding:utf-8-*-
def test_print (algorithm, ListName, target):
  print ('%d is%s in %s '% (target, ' if Algorithm (eval (listname), target) Else ' not ', listname)
if __name__ = = ' __main__ ':
  testlis t = [1, 2, 8, MB, orderedlist, 0]
  = sorted (testlist)
  print (' Sequentialsearch: ')
  Test_print (sequ Entialsearch, ' Testlist ', 3
  test_print (sequentialsearch, ' testlist ', ')
  print (' Orderedsequentialsearch: ')
  test_print (orderedsequentialsearch, ' orderedlist ', 3)
  test_print (Orderedsequentialsearch, ' Orderedlist ', ('
  iterativebinarysearch: ')
  test_print (iterativebinarysearch, ' orderedlist ', 3)
  test_print (iterativebinarysearch, ' orderedlist ')
  print (' Recursivebinarysearch: ')
  test_print ( Recursivebinarysearch, ' Orderedlist ', 3
  test_print (recursivebinarysearch, ' orderedlist ', 13)

Run Result:

$ python testbasicsearch.py
Sequentialsearch: 3 is isn't in Testlist's in
 testlist
Orderedsequentialsearch: 3 is isn't in
 orderedlist
-orderedlist iterativebinarysearch:
 3 is isn't in Orderedlist
-orderedlist recursivebinarysearch: 3 is isn't in
 orderedlist # in
 orderedlist

I hope this article will help you with your Python programming.

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.