A basic search of data structures and algorithms implemented by Python

Source: Internet
Author: User
This paper describes 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: from the beginning of the list to the end, compare the items to be searched and the list, until the target item (search succeeds) is found or the search is out of scope (search failed).

Depending on whether the items in the list are sorted in order, you can divide the list into unordered lists and ordered tables . For unordered lists, exceeding the search scope is the end of the list; For an ordered list, more than the search scope refers to the range that is larger than the target item in the list (when the target item is less than the end of the list) or to the end of the list (when the target item is greater than the item at the end of the list).

1. Unordered list

To sequence a search in an unordered list:

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

2. Ordered list

To sequence a search in an ordered list:

def orderedsequentialsearch (items, target): For  item in items:    if item = = target:      return True    elif Item & Gt Target:      break  return False

Two or two points search

In fact, the above-mentioned Orderedsequentialsearch algorithm does not make good use of the characteristics of a sequential list.

binary search takes advantage of an ordered list, and the idea is ingenious: in the original list, the target is compared to the list intermediate (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.

In the case of a binary search in an ordered list:

Depending on the implementation approach, the binary search algorithm can be divided into iterative versions and recursive versions of two:

1. Iteration Version

def iterativebinarysearch (items, target): First  = 0 Last  = Len (items)-1 when first  <= last:    middle =  (first + last)//2    if target = = Items[middle]:      return True    elif target < Items[middle]: last      = middle -1    Else: first      = 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:

The          time complexity of the search algorithm-----------------------------------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 important to note that the time complexity of the member operator in is O (n), it is not difficult to guess, the 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 '% (tar Get, ' If Algorithm (eval (listname), target) Else ' not ', listname)) if __name__ = = ' __main__ ':  testlist = [1, 2, 32, 8 , 0]  orderedlist = sorted (testlist)  print (' Sequentialsearch: ')  test_print ( Sequentialsearch, ' Testlist ', 3  test_print (sequentialsearch, ' testlist ', ')  print (' Orderedsequentialsearch: ')  test_print (orderedsequentialsearch, ' orderedlist ', 3)  Test_print ( Orderedsequentialsearch, ' orderedlist ',  print (' Iterativebinarysearch: ')  test_print ( Iterativebinarysearch, ' Orderedlist ', 3  test_print (iterativebinarysearch, ' orderedlist ', ')  print (' Recursivebinarysearch: ')  test_print (recursivebinarysearch, ' orderedlist ', 3)  Test_print ( Recursivebinarysearch, ' orderedlist ', 13)

Operation Result:

$ Python testbasicsearch.pysequentialsearch:3 isn't in testlist are in Testlistorderedsequentialsearch:3 are not in or Deredlist-Orderedlistiterativebinarysearch:3 is in Orderedlist and Orderedlistrecursivebinarysearch:3 Isn't in Orderedlist-in orderedlist

Hopefully this article will help you with Python programming.

  • Related Article

    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.