Professional Documents
Culture Documents
DSU Microproject
DSU Microproject
PROJECT REPORT
ON
BY
2. Linear Search 3.
3. Linear Search Algorithm 7.
1) Linear search:-
It is also called as Sequential search.
It is the simplest searching technique.
In this technique we compare the search element with first element in the list.
If they are same then the search is Successfull. If they are do not match then we compare
search element with Second element and so on until the element is match .
Also it may happens we may reach the end of the list without matching the search
element with the element in the list
In such conditions the search is said to be Unsuccessful
It is applied on Unsorted data.
Linear search has some complexities as given below:
Space Complexity:
Space complexity for linear search is O(n) as it does not use any extra space where n is
the number of elements in an array.
Time Complexity:-
*Best- case complexity = O(1) occurs when the search element is present at the first
element in the search array
Worst- case complexity = O(n) occurs when the search element is not present in the set
of elements or array
Average complexity = O(n) is referred to when the element is present somewhere in the
search array.
1)Example:-
Let’s take an array of elements as given below:
10,15,5,16,4,5
Search element=16.
Step 2:- we compare search element (16) with Second element (15)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
16 is not equal to 15
No match
Move to next element
Step 3:- we compare search element (16) with Third element (5)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
16 is not equal to 5
No match
Move to next element
Step 3:- we compare search element (16) with fourth element (16)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
16 = 16
Match Found
Element found at index position 3
2)Example:-
Let’s take an array of elements as given below:
10,15,5,16,4,5
Search element=2.
Step 1: we compare search element (2) with first element (10)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 10
No match
Move to next element
Step 2: we compare search element (2) with second element (15)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 10
No match
Move to next element
Step 3: we compare search element (2) with second element (15)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 15
No match
Move to next element
Step 4: we compare search element (2) with third element (5)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 5
No match
Move to next element
Step 5: we compare search element (2) with Fourth element (16)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 16
No match
Move to next element
Step 5: we compare search element (2) with Fifth element (4)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 4
No match
Move to next element
Step 6: we compare search element (16) with last element (5)
0 1 2 3 4 5
10 15 5 16 4 5
INDEX: 0 1 2 3 4 5
DATA: 10 15 5 16 4 5
2 is not equal to 5
No match
Element Not Found in this list.
ALGORITHMS:-
Linear Search ( Array A, Value x)
Step 1: Set I to 1
Step 2: if I > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set I to I + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index I and go to step 8
Step 7: Print element not found
Step 8: Exit
Flowchart:-
Program :-
#include<studio.h>
void main()
{
Int a[100],flag=0,I,key,pos,n;
clrscr();
printf(“ENTER THE SIZE OF ARRAY”);
scanf(“%d”,&n);
printf(“ENTER THE %d ELEMENT “,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ENTER THE SEARCH ELEMENT”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
{
If(a[i]==key)
{
flag=1;
pos=i;
break;
}
}
If(flag==1)
{
printf(“ELEMENT FOUND AT INDEX POSITION %d”,pos);
}
else
{
printf(“ELEMENT NOT FOUND”);
}
getch();
}
2) BINARY SEARCH:-
This algorithm finds specific items by comparing the middlemost items in the data
collection. When a match occurs, it returns the index of the item. When the middle item
is greater than the item, it searches for a central item of the left sub-array. In contrast, if
the middle item is smaller than the search item, it explores the middle of the item in the
right sub-array. It continues searching for an item until it finds it or until the sub-arrays
size becomes zero.
Binary search needs sorted order of items. It is faster than a linear search algorithm.
Run-time complexity = O(log n)
The binary search algorithm has complexities as given below:
Worst-case complexity = O (n log n)
Average complexity = O (n log n)
Best case complexity = O (1)
EXAMPLE:-
FIND THE ELEMENT 4 IN THE LIST
A={2,4,9,15,45,65,78,80}
0 1 2 3 4 5 6 7
2 4 9 15 45 65 78 80
Search element =9
Firstly we find the middle index
Start=0
Start=7
Formula=(start+end)/2
=(0+7)/2
=3.5
So we take 3
Step 1:-
We compare the search element (9) with middle element (15)
Index= 0 1 2 3 4 5 6 7
Data= 2 4 9 15 45 65 78 80
15 is not equal to 9
BOTH ARE NOT MATCHING & SEARCH ELEMENT IS SMALLER THAN THE
MIDDLE ELEMENT SO WE WILL SEARCH IN THE “LEFT SUBLIST” i.e (2,4,9)
INDEX=0 1 2
DATA= 2 4 9
Middle = (0+2)/2
=1
STEP 2:-
We compare the search element (9) with middle element (4)
INDEX=0 1 2
DATA= 2 4 9
4 is not equal to 9
BOTH ARE NOT MATCHING & SEARCH ELEMENT IS GREATER THAN THE
MIDDLE ELEMENT SO WE WILL SEARCH IN THE “RIGHT SUBLIST” i.e(9)
STEP 3:-
AS ONLY ONE ELEMENT IS REMAINING WE COMPARE THE SEARCH ELEMENT
(9) WITH REMAINING ELEMENT
INDEX= 2
DATA= 9
MATCH FOUND
ELEMENT FOUND AT INDEX POSITION 2
ALGORITHM:-
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
Set pos = mid
Print pos
Go to step 6
Else if a[mid] > val
Set end = mid – 1
Else
Set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
Print “value is not present in the array”
[end of if]
Step 6: exit
FLOWCHART:-
PROGRAM:-
include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf(“Enter number of elements\n”);
scanf(“%d”, &n);
printf(“Enter %d integers\n”, n);
for (c = 0; c < n; c++)
scanf(“%d”, &array[c]);
printf(“Enter value to find\n”);
scanf(“%d”, &search);
first = 0;
last = n – 1;
middle = (first+last)/2;
while (first <= last) {
If (array[middle] < search)
First = middle + 1;
else if (array[middle] == search) {
printf(“%d found at location %d.\n”, search, middle+1);
break;
}
else
Last = middle – 1;
Middle = (first + last)/2;
}
If (first > last)
printf(“Not found! %d isn’t present in the list.\n”, search);
return 0;
CONCLUTION:
IN THIS MICROPROJECT WE CONCLUDE THAT how Data structures help us to
manage large amounts of data, such as huge databases. Efficient data structures are the
fundamental basis for efficient algorithms. Besides efficient storage, data structures are also
responsible for the efficient retrieval of data from stored locations. It includes an array,
Graph, Searching, Programs, Linked List, Pointer, Stack, Queue, Structure, Sorting, and so
forth.
The concepts of searching in a data structure, as well as its methods, are covered in this
Microproject
REFERANCE:-
Websites:-
https://www.geeksforgeeks.org/binary-search/
https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorit
hm.htm#:~:text=Linear%20search%20is%20a%20very,end%20of%20the%20dat
a%20collection.
Images:-
https://www.google.com/search?q=linear+search+flowchart&sxsrf=ALiCzsa13xr
X3XvA4ar3wkoa3oQf95qXXQ:1670593524337&source=lnms&tbm=isch&sa=
X&ved=2ahUKEwju0NPs1ez7AhUKSWwGHRYfAXgQ_AUoAXoECAIQAw
&biw=1600&bih=789#imgrc=eLzzeDwFGFWulM