community
directory
books
authors
images
encyclopedia

Email:
Password:
Register

Knowledgerush Search

 

Google
  Web knowledgerush


Search for images of Linear search


Message boards   Post comment

Linear search

In the field of computer science, linear search is a search algorithm, also known as sequential search, that is suitable for searching a set of data for a particular value.

It operates by checking every element of a list until a match is found. Linear search runs in O(N). If the data are distributed randomly, on average N/2 comparisons will be needed. The best case is that the value is equal to the first element tested, in which case only 1 comparison is needed. The worst case is that the value is not in the list, in which case N comparisons are needed.

Here is a sample implementation in Ruby:

def linear_search(array,value)
    for i in array
        return true if i == value;
    end
    return false
end

Here is a sample implementation in PHP:

function linear_search($array, $value)

Linear search can be used to search an unordered list. The more efficient Binary search can only be used to search an ordered list.

If more than a small number of searches are needed, it is advisable to use a more efficient data structure. One approach is to sort and then use binary searches. Another common one is to build up a Hash table and then do hash lookups.

Referenced By

List of algorithms | List of combinatorics topics | List of mathematical topics (J-L) | Ruby language | Ruby programming language

 

Compose Your Message

Your Email Address or Pen Name (optional):
Subject:
Your Message:
 

 

 

 

 

 

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Linear search".

 

Contact UsPrivacy Statement & Terms of Use

 
Copyright © 1999-2003 Knowledgerush.com. All rights reserved.