Example : A Searching Example/program in C to show the Linear/Sequential Search using Array. [Searching in the Array].
#include <stdio.h>
#include <conio.h>
void main()
{
int x[10],i,k,found=0;
printf ("Enter ten values in an array : ");
for (i=0; i<=9; i++)
{
scanf ("%d", &x[i]);
}
printf ("Enter the value we want to search from the array : ");
scanf ("%d", &k);
for (i=0; i<=9; i++)
{
if(x[i]==k)
{
found=found+1;
}
}
if(found>0)
{
printf("Value Found");
}
else
{
printf("Value Not Found");
}
getch();
}
----------------------- OR ------------------------
#include <stdio.h>
#include <conio.h>
void main()
{
int x[10];
int i, j, temp=0;
printf("Enter ten values for array: ");
for(i=0; i<10; i++)
{
scanf("%d", &x[i]);
}
printf("\n Enter a value to search in an array: ");
scanf("%d", &j);
for(i=0; i<10; i++)
{
if(x[i]==j)
{
temp++;
}
}
if(temp==1)
{
printf("\n value %d is found at position %d", j, i + 1);
}
if(temp==0)
{
printf("\nValue %d is not found in the array", j);
}
if(temp>=2)
{
printf("\n More than one value is found in the array");
}
getch();
}
Example: A computer program in C to show the Linear/Sequential Search using Linked List. [Searching in the Linked List].
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void search(struct number *);
int i,x,y;
void main()
{
printf("Enter the value for nodes in Linked List = ");
scanf("%d",&x);
home= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&home->num);
home->ptr=NULL;
create(home);
search(home);
getch();
}
void create(struct number *home1)
{
link=home1;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=NULL;
link->ptr=newnode;
link=link->ptr;
}
}
void search(struct number *home2)
{
int count=0;
printf("\nEnter the searched value = ");
scanf("%d",&y);
for(link=home2;link!=NULL;link=link->ptr)
{
if(link->num==y)
count++;
}
if(count==0)
printf("\nValue Not found");
else
printf("\nValue found");
}
Output :
Enter the value for nodes in Linked List = 3
Enter Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter the searched value = 30
Value found
Example: Algorithm for Linear Search.
- The linear search algorithm, also known as sequential search, is a straightforward search algorithm that checks every element in a list or array to find a specific target element.
- The linear search operates on a collection of elements, which could be an array, a list, or any ordered sequence of elements.
- The steps involved in performing a linear search are as follows:-
Step1: Iterate through Elements:
-
-
- Start iterating through each element one by one in the unsorted list/array from the beginning or first element at index 0 and proceed through each subsequent element.
-
Step2: Compare Each Element with the Target/Finding value:
-
-
- Check if the current element being inspected matches the target element being searched for.
- If the element matches the target:
- Return the index of the element where the match occurred.
- If the element doesn’t match:
- Move on to the next element in the list.
- If the element matches the target:
- Check if the current element being inspected matches the target element being searched for.
-
Step3: Repeat the Process (Step2)Until the Target/Finding Value is Found or the List Ends without Matched.
Step4: Return Result:
-
-
- If the target element is found, return its index in the list.
- If the target element is not found, return a signal (-1) which indicates that the element does not exist in the list.
-
- Pseudocode:
Example: Algorithm for Binary Search.
- The binary search algorithm is a commonly used search algorithm that finds the position/location of a target or finding value within a sorted array.
- The algorithmic steps of a binary search are as follows:-
-
-
Initialization Process:
- Define the target or finding value that we want to find within the array.
- Sort the Array (either in ascending or descending order) first within which we want to search.
-
Set Pointer Position or Location:
- Initialize two pointers,
start
andend
, pointing to the start index and end index of the array respectively. - Calculate the
mid
index as(start + end) / 2
.
- Initialize two pointers,
-
Searching Process:
- Compare the target or finding value with the element at the
mid
index of the array. - If the target or finding value matches the element at the
mid
index, the search is successful and returns themid
index. - If the target or finding value is less than the element at the
mid
index, update theend
pointer tomid - 1
and recalculatemid
index. - If the target or finding value is greater than the element at the
mid
index, update thestart
pointer tomid + 1
and recalculatemid
index. - Repeat these steps until the target or finding value is found or until the
start
pointer exceeds theend
pointer.
- Compare the target or finding value with the element at the
-
Termination:
- If the search ends with the
start
pointer exceeding theend
pointer, then the target or finding value is not present or matched in the sorted array.
- If the search ends with the
-
For Example:
Suppose we have an array [2, 4, 16, 8, 14, 10, 12, 6]
and we want to search for a value 10
in it.
-
-
- First of all sort the given array in some specific order say in ascending or descending order as per need.
- Initialization:
target = 10
, sorted array =[2, 4, 6, 8, 10, 12, 14, 16]
- Pointers:
start index = 0
,end index = 7
mid index = (start index + end index )/2 = (0 + 7) / 2 = 3
(i.e., the value at index 3 is 8 and not matched with finding value 10)- Since
10 > 8
, updatestart index = (previous) mid + 1 = 4
current mid = (4 + 7) / 2 = 5
(value at index 5 is 12 which is also not matched with finding value 10)- Since
10 < 12
, updateend index = mid - 1 = 4
- After
start
andend
pointers converge, and the value10
is found at the index4
.
-
Example : A computer program in C to show the Binary Search using Array.
#include<stdio.h>
#include<conio.h>
int main()
{
int i, first, last, midpoint, n, y, x[50];
printf("Enter the size (less than 50) of an array\n");
scanf("%d", &n);
printf("Enter %d integer numeric values for array\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &x[i]);
}
//sorting the array values
for(i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(x[i]>x[j])
{
int temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("Now enter specific value U want to search\n");
scanf("%d", &y);
// Binary search codes
first = 0;
last = n-1;
midpoint = (first+last)/2;
while (first <= last)
{
if (x[midpoint] < y)
first = midpoint + 1;
else if (x[midpoint] == y)
{
printf("The search value %d found at location %d.\n", y, midpoint+1);
break;
}
else
last = midpoint - 1;
midpoint = (first + last)/2;
}
if (first > last)
printf("The search value %d is not found/present in the array list.\n", y);
return 0;
}
Output:
Enter the size (less than 50) of an array
4
Enter 4 integer numeric values for array
62
30
800
2
Now enter specific value U want to search
2
The search value 2 found at location 1.
0 Comments