Home     Blog

Linear Search

This method traverses a list sequentially to locate the search key.

The algorithm that one chooses generally depends on the organization of the array elements. If the elements are in random order, then one should choose the linear search technique.

Algorithm

linearsearch (a,n,item,loc)

Here "a" is an array of the size n. This algorithm finds the location of the element "item" in the array "a". If search item is found, it sets loc to the index of the element; otherwise, it sets loc to -1

Begin for i=0 to (n-1) by 1 do if (a[i] = item) then set loc=I exit endif endfor set loc -1 end

C/C++ Implementation of the Algorithm

.cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 { color: green; } .cb2 { color: blue; } .cb3 { color: maroon; }

int linearsearch (int a[], int n, int key)
{
 int i;
 for(i=0;i<n;i++)

linear search Linear Search

 {
 if(a[i]==key)
 return i;
 }
 return -1;
}

Analysis of Linear Search

In the best possible case, the item may be found at the first position. In that case, the search operation terminates in success with just one comparison.

The worst case occurs when the item is present at the last position or it is missing from the array. In the former case, the search terminates in success with n comparisons. In the latter case, the search terminates in failure with n comparisons. Thus, we find that in the worst case, linear search carries out n operations.

Complete Program

/**
 * Program that illustrates the working of Linear Search
 * http://www.tech-faq.com
 */
 
#include <stdio.h>
 
/**
 * Parameters:
 * a: array to search in
 * n: number of elements in the array
 * key: search key
 *
 * Return Value: index of the first occurence
 * of key in a[], or -1 if not found
 */
int linearsearch (int a[], int n, int key)
{
 int i;
 for(i=0;i<n;i++)
 {
 if(a[i]==key)
 return i;
 }
 return -1;
}
 
 
int main()
{
 int array[50], num, index, element, key;
 
 /*Fill the array*/
 puts("How many elements do you want to create the array with? (Max 50)");
 scanf("%d", &num);
 
 printf("Enter the elements: ");
 fflush(stdout);
 
 for (index=0; index<num; index++) 
 scanf("%d", &array[index]);
 
 printf("Enter the search key: ");
 fflush(stdout);
 scanf("%d", &key);
 
 if ( (index=linearsearch(array, num, key)) != -1)
 printf("The element was found at index: %dn", index);
 else
 printf("Could not find %dn", key);
 
 return 0;
}

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Binary Search

    This method works on sorted lists by progressively making better guesses to find the location of a search key. Illustration Consider the following array: 3 10 15 20 35 40 60 Suppose we want to search the element "15" We take beg = 0, end = 6 and compute the location of the middle element [...]...


  • Linear Queue

    Implementation of operations on a linear queue: Creating an Empty Queue Before we can use a queue, it must be created. The purpose of initializing the queue is served by assigning -1 (as a sentinel value) to the front and rear variables. Note that the valid range of an index for the array is 0 [...]...


  • Traversing and Searching a Linear Linked List

    Traversing a list A linear list can be traversed in two ways In order traversal Reverse order traversal In order Traversal To traverse the linear linked list, we walk the list using the pointers, and process each element until we reach the last element. .cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; [...]...


  • Insertion in a Linear Linked List

    To insert an element in the list, the first task is to create a new node, assign the element to be inserted to the info field of the node, and then place the new node at the appropriate position by adjusting the appropriate pointers. Insertion in the list can take place at the following positions: [...]...


  • Doubly Linked List – Traversing and Search

    Traversing a Doubly Linked List A doubly linked list can be traversed either way and that too very conveniently. Inorder traversal Reverse order traversal Inorder Traversal To traverse the doubly linked list, we walk the list from the beginning, and process each element until we reach the last element. .cf { font-family: Lucida Console; font-size: [...]...