DATA STRUCTURE - Searching methodes

 Course-Data Structures | Talenthome

Searching methods

                In data structure there are searching methods applying to search element. There are two searching methods are available for searching data element in two different ways.

 

1)    Linear search –

This is the first and basic searching techniques. According to its name in this searching method we have to follow sequence, i.e., we have to check each and every data element of data structure one by one. It doesn’t matter how many data elements are there in data structure. We have to check all data element because we cannot directly go to the data element for which we are looking. There is one disadvantage of this searching method that this is time consuming searching method, because it takes time to compare each and every element for the required data.

Algorithm for linear search

 

1.       Initialize counter, i=1 to n

2.       if LA[i]=item

3.       print element found at I position

4.       else

print element not found

5.       exit .

 

 

 

2)    Binary search –

This searching method is more convenient and faster as compared to linear search but there is one condition that the array of data structure must be in ascending order. Binary search methods divide data structure in two parts. These methods always check/searches the element at the middle position. If element did not found that middle position, that whether the element is greater or lesser than the element located at middle position.

Then first or last definition will change and as per changed value mid is calculated and again binary search check that whether the element is available at mid or not and finally this process goes continuously filled up.

Algorithm for binary search

 

1.       Define low & high.

2.       Do mid=low+high/2

3.       If v=LA[mid], return LA[mid]

4.       If v>LA[mid], low=mid+1

5.       else, high=mid-1

6.       return nil

 

Comments

Popular posts from this blog

Theory part introduction

Data structure - Sorting methodes