Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
LINEAR SEARCH ALGORITHM
Algorithm involves checking all the elements
of the array(or any other structure) one by
one and i...
Daily life example
If you are asked to find the name of the person having
phone number say “1234” with the help of a telep...
Best case
● If the first number in the directory is the number you
were searching for ,then lucky you!!.
● Since you have ...
Worst Case
It may happen that the number you are searching for is the last
number of directory or if it is not in the dire...
Pseudocode
For all elements
Check if it is equal to element being searched
for.
If it is ,return its position.
else contin...
C++ code [Iterative]
void iterSearch(const double data [ ],int n,double key) //const for safety ,we want to keep array unc...
Recursive Linear Search
bool recursiveSearch (const double data[ ],int n,double key)

{
static int i=0;

//static will pre...
Discussions
1.Sorted array is not needed.
2.Works fine for small number of elements .Search time
increases with number of ...
Upcoming SlideShare
Loading in …5
×

Linear search algorithm

23,004 views

Published on

Linear Search Algorithm .Examples.Pseudo-code,C++Implementation and Discussions..

Published in: Education, Technology, Business
  • Dating direct: ❤❤❤ http://bit.ly/2u6xbL5 ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ http://bit.ly/2u6xbL5 ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • If you have any doubt in Linear search Algorithm or you find any error in this presentation or any ambiguity .Feel free to discuss in comments.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Linear search algorithm

  1. 1. LINEAR SEARCH ALGORITHM Algorithm involves checking all the elements of the array(or any other structure) one by one and in sequence until the desired result is found.
  2. 2. Daily life example If you are asked to find the name of the person having phone number say “1234” with the help of a telephone directory . Since telephone directory is sorted by name not by numbers,we have to go through each and every number of the directory
  3. 3. Best case ● If the first number in the directory is the number you were searching for ,then lucky you!!. ● Since you have found it on the very first page,now its not important for you that how many pages are there in the directory. ● Whether if it is of 1000 pages or 2000 pages it will take u same time to find you the number ,if it is at the very beginning . ● So it does not depends on no. on elements in the directory.Hence constant time . InBig O notation : 0(1)
  4. 4. Worst Case It may happen that the number you are searching for is the last number of directory or if it is not in the directory at all. In that case you have to search the whole directory. Now number of elements will matter to you.if there are 500 pages ,you have to search 500;if it has 1000 you have to search 1000. Your search time is proportional to number of elements in the directory. In big O notation O(n) No of elements No of comparisons to be done 15 15 600 600
  5. 5. Pseudocode For all elements Check if it is equal to element being searched for. If it is ,return its position. else continue.
  6. 6. C++ code [Iterative] void iterSearch(const double data [ ],int n,double key) //const for safety ,we want to keep array unchanged { for(int i=0;i<=n-1;i++) //looping through all elements of the array { if(data[i]==key) { cout<<key<<" found at index "<<i<<" of the array"<<endl; break; //if element is found,come out of the loop } if(i==n-1) //searched through the array,still not found cout<<"n Element not found n"; } }
  7. 7. Recursive Linear Search bool recursiveSearch (const double data[ ],int n,double key) { static int i=0; //static will prevent i being initialised to 0 every time we enter the function if(data[i]==key) { cout<<key<<" found at index "<<i<<" of the array"<<endl; return true; //this will end recursion which is desired as the element has been found } else { ++i; if(i==n) { cout<<"nElemnent not foundn"; } recursiveSearch(data,n,key); } } i=0; return false; //i=0 to reset i for next search.
  8. 8. Discussions 1.Sorted array is not needed. 2.Works fine for small number of elements .Search time increases with number of elements. 3.Elements with higher probability of being searched should be kept in the beginning. 4.Trick: Get completely rid of the end-check, namely putting a sentinel value after the end of the last valid array element.

×