Friday, 1 December 2017

Write and explain linear search procedure or algorithm with a suitable example.

Linear search technique is also known as sequential search technique. The linear search is a method of searching an element in a list in sequence. In this method, the array is searched for the required element from the beginning of the list/array or from the last element to first element of array and continues until the item is found or the entire list/array has been searched.

Algorithm of Linear search:
Step 1: set-up a flag to indicate “element not found”.
Step 2: Take the first element in the list
Step 3: If the element in the list is equal to the desired element
a. Set flag to “element found”
b. Display the message “element found in the list”
c. Go to step 6
Step 4: If it is not the end of list,
a. Take the next element in the list
b. Go to step 3
Step 5: If the flag is “element not found”
Display the message “element not found”
Step 6: End of the Algorithm

Advantages of Linear search::
1. It is simple and conventional method of searching data. The linear or sequential name implies that the items are stored in a systematic manner.
2. The elements in the list can be in any order. i.e. The linear search can be applied on sorted or unsorted linear data structure.

Disadvantage of Linear search: :
1. This method is insufficient when large number of elements is present in list.
2. It consumes more time and reduces the retrieval rate of the system.

Time complexity: O(n) 

Mukesh Rajput

No comments:

Post a Comment

Mukesh Rajput