Introduction

 

Searching Algorithms


The searching algorithms are used to search or find one or more than one element from a dataset. These type of algorithms are used to find elements from a specific data structures.

Searching may be sequential or not. If the data in the dataset are random, then we need to use sequential searching. Search algorithms are designed to retrieve something or retrieve an object from any data structure in which it is stored. Depending on the type of search function, these algorithms are usually divided into two categories:-

 Sequential Search: In this case, the list of elements are traverse by sequence to find a value and if the desire value is present, it will show. 

 Interval Search: These algorithm are used for searching in sorted data structures. This type of search algorithms works much better than Linear Search as they often point to the center of the search structure and divide the search space in half.


1. Linear search:- 

Linear search is the easiest way to search. In this type of search, you have to traverse sequentially. Every element is checked and when a match is found the item is returned, otherwise the search continues until the end of data collection.

 Linear search algorithm where A is array and X is the value:-

Step 1: Set i to 1.

Step 2: if i > n then go to step 7.

Step 3: if A[i] = x then go to step 6.

Step 4: Set i to i + 1.

Step 5: Go to Step 2.

Step 6: Print Element x Found at index i and go to step 8.

Step 7: Print element not found.

Step 8: Exit

Example:

 

Linear search for 20.

Linear search is the most common searching algorithm. Its biggest advantage is that it’s ready-to-use — it just always works, no extra prerequisites. It comes with a cost of time though.

Linear search doesn’t care if the collection is sorted or not, it just goes one-by-one element and checks if it matches our key (key is an element I’m looking for). The worst case scenario for this algorithm to find a key is O(n) where n is a number of elements. It will happen in two cases:
1. Key element is the last one
2. Key element is not present



 Linear search

 

Comments

  1. Understand linear search completely, blog in simple language. Keep it up.

    ReplyDelete
  2. Explain in simple words... really Great...

    ReplyDelete
  3. Great work. Simple and informative

    ReplyDelete
  4. Very informative and knowledgeable
    Great work

    ReplyDelete

Post a Comment

Popular posts from this blog

Interpolation Search and Exponential Search

Binary Search and Jump Search