searches.linear_search¶

This is pure Python implementation of linear search algorithm

For doctests run following command: python3 -m doctest -v linear_search.py

For manual testing run: python3 linear_search.py

Attributes¶

user_input

Functions¶

linear_search(→ int)

A pure Python implementation of a linear search algorithm

rec_linear_search(→ int)

A pure Python implementation of a recursive linear search algorithm

Module Contents¶

searches.linear_search.linear_search(sequence: list, target: int) → int¶

A pure Python implementation of a linear search algorithm

Parameters:
  • sequence – a collection with comparable items (as sorted items not required in Linear Search)

  • target – item value to search

Returns:

index of found item or -1 if item is not found

Examples: >>> linear_search([0, 5, 7, 10, 15], 0) 0 >>> linear_search([0, 5, 7, 10, 15], 15) 4 >>> linear_search([0, 5, 7, 10, 15], 5) 1 >>> linear_search([0, 5, 7, 10, 15], 6) -1

searches.linear_search.rec_linear_search(sequence: list, low: int, high: int, target: int) → int¶

A pure Python implementation of a recursive linear search algorithm

Parameters:
  • sequence – a collection with comparable items (as sorted items not required in Linear Search)

  • low – Lower bound of the array

  • high – Higher bound of the array

  • target – The element to be found

Returns:

Index of the key or -1 if key not found

Examples: >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 0) 0 >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 700) 4 >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, 30) 1 >>> rec_linear_search([0, 30, 500, 100, 700], 0, 4, -6) -1

searches.linear_search.user_input¶

thealgorithms-python

Navigation

index.md

  • Contributing guidelines
  • Getting Started
  • Community Channels
  • List of Algorithms
  • MIT License
  • API Reference
    • maths
    • other
    • sorts
    • graphs
    • hashes
    • matrix
    • ciphers
    • geodesy
    • physics
    • quantum
    • strings
    • fractals
    • geometry
    • graphics
    • knapsack
    • searches
    • financial
    • blockchain
    • scheduling
    • conversions
    • electronics
    • fuzzy_logic
    • backtracking
    • audio_filters
    • file_transfer
    • project_euler
    • greedy_methods
    • linear_algebra
    • neural_network
    • boolean_algebra
    • computer_vision
    • data_structures
    • networking_flow
    • web_programming
    • bit_manipulation
    • data_compression
    • machine_learning
    • cellular_automata
    • genetic_algorithm
    • divide_and_conquer
    • linear_programming
    • dynamic_programming
    • digital_image_processing

Related Topics

  • Documentation overview
    • API Reference
      • searches
        • Previous: searches.jump_search
        • Next: searches.median_of_medians
©2014, TheAlgorithms. | Powered by Sphinx 8.2.3 & Alabaster 1.0.0 | Page source