Search This Blog

Sunday, April 19, 2020

Searching on array in C-Programming

                                      C-Programming:-

In this tutorial yon will learn: 

* Searching  :- 

 

 The process of finding on element in an array is known as searching.

There are two types of searching are:-

1 Linear searching :-  Linear searching is also known as sequential search in linear search process we will find out an element from lower index one by one in an array.\

let we take one example :-

Write a program to find out the existence and position of a given element is an array.

#includestdio.h>
#include<conio.h>
void main()
{
int  a [5] = { 10 , 20 , 30 , 40 , 50 };
int n;
printf("enter no you want to search " );
scanf("%d",&n);
for( i=0 ; i<5  ; i++)
{
if (a [i]=n);
{
printf("%d is present location is ", n , i++) ;
}
else
{
printf("not present" ) ;
 }
}
getch() ;
}

 * Existence and sequence :-

Take one example  :-

#include<stdio.h>
#include<conio.h>
void mian()
{
int a [5] ={ 10 , 20 , 30 , 40 , 50 };
int n, i count= 0;
printf("Enter number you want to search");
scanf("%d",&n);
for(i=0 ; i<5 ; i++)
{
if ( a[5]==n)
{
count ++;
}
if ( count>0)
{
printf("element found % d", count);
}
else
{
printf("element not found");
}
getch();
}

2 Binary searching :

                                      Binary search algorithm applies to a sorted array for searching an element 
* sort ( ascending)
* Find the middle value / element
compare mid value to given number :-
match                                               [ Greater ------------- Right]
no match                                          [ Lesser  -------------- Left]

                                  Steps of binary searching again:-   

Binary search is the fastest search we can perform binary process in fallowing:-

* Sort the given array in ascending order.
* Find the mid element of an array .  
       Lowe index  +  Higher index /  2 
Compare mid element with given element want to be search.
Part ( a ) -  If no matched with the mid element  .   Print "element found " .
Part ( b ) - If mid element not matched then compare element is greater  or  lesser.
Part ( c ) - If value is greater start search in right list of an array .  If value is lesser start in left list of array.
                                                         ( Or in simple search) 
Perform sorting ( ASC )
Find mid element 
Compare mid value to given  number.
Value matched                  ( Element found)
If value is not matched --
( a ) Value is greater   ( search in right )
( b ) Value is lesser    ( search in left ) 

Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

0 comments:

Post a Comment

If you have any doubt, please let me know