Dsu MP
Dsu MP
MICRO PROJECT:
TITLE OF THE PROJECT- SEARCHING METHODS
1
MAHARASHTRA STATE BOARD OF
TECHNICAL EDUCATION
CERTIFICATE
2
Group Details
3
INDEX
1 Certificate 2
2 Group Details 3
3 Index 4
4 Annexure IA (part A) 5
6 Linear search 7
7 Binary search 14
8 Annexure II A (Part B) 21
4
JSPM’s
JAYAWANTRAO SAWANT POLYTECHNIC,
Handewadi Road, Hadapsar, Pune-28
Department of Computer Engineering
Academic Year 2023-24
4.0 Resources required (major resources such as raw material, some machining facility,
software etc.)
Sr. No. Name of resource Specification Quantity Remarks
1 Laptop/Computer Windows 11 1
Processor 8GB RAM
3 Other - -
5
6
TITLE OF THE PROJECT: SEARCHING METHODS
❖ Searching: -
Searching in data structure refers to the process of finding the required information from a
collection of items stored as elements in the computer memory. These sets of items are in
different forms, such as an array, linked list, graph, or tree. Another way to define searching
in the data structures is by locating the desired element of specific characteristics in a
collection of items.
❖ Types of searching: -
The list or array of elements is traversed sequentially while checking every component of
the set.
The interval search includes algorithms that are explicitly designed for searching in sorted
data structures. In terms of efficiency, these algorithms are far better than linear search
algorithms.
7
▪ Linear search/sequential search: -
The linear search algorithm iteratively searches all elements of the array. It has the best
execution time of one and the worst execution time of n, where n is the total number of items
in the search array.It is the simplest search algorithm in data structure and checks each item
in the set of elements until it matches the searched element till the end of data collection.
When the given data is unsorted, a linear search algorithm is preferred over other search
algorithms.
● Every element is considered as a potential match for the key and checked for the same.
● If any element is found equal to the key, the search is successful and the index of that
element is returned.
● If no element is found equal to the key, the search yields “No match found”.
● For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90, 40} and key = 3
Step 1: Start from the first element (index 0) and compare key with each element (arr[i]).
Comparing key with first element arr[0]. SInce not equal, the iterator moves to the next
element as a potential match.
Comparing key with next element arr[1]. SInce not equal, the iterator moves to the next
element as a potential match.
8
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search
Algorithm will yield a successful message and return the index of the element when key is
found (here 2).
9
▪ Complexities in linear search are: -
▪ Space Complexity
Since linear search uses no extra space, its space complexity is O(n), where n is the
number of elements in an array
▪ Time Complexity
Best-case complexity = O(1) occurs when the searched item is present at the first element
in the search array.
Worst-case complexity = O(n) occurs when the required element is at the tail of the array
or not present at all.
Average- case complexity = average case occurs when the item to be searched is in
somewhere middle of the Array.
10
▪ Algorithm of linear search/sequential search
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
11
▪ C PROGRAM OF LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,key,n;
clrscr();
printf("\n Enter size of array");
scanf("%d",&n);
printf("\n Enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element to be searched");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("Element found at index %d \n",i);
break;
}
}
if(i==n)
{
printf("\nElement not found in array");
}
getch();
}
12
▪ OUTPUT
13
▪ Advantages of Linear Search
1. Linear search can be used irrespective of whether the array is sorted or not. It can be used
on arrays of any data type.
2. Does not require any additional memory.
3. It is a well-suited algorithm for small datasets.
1. Linear search has a time complexity of O(N), which in turn makes it slow for large
datasets.
2. Not suitable for large arrays.
14
15
▪ Binary search/interval search: -
This algorithm locates specific items by comparing the middlemost items in the data
collection. When a match is found, it returns the index of the item. When the middle item is
greater than the search item, it looks for a central item of the left sub-array. If, on the other
hand, the middle item is smaller than the search item, it explores for the middle item in the
right sub-array. It keeps looking for an item until it finds it or the size of the sub-arrays
reaches zero.
Binary search needs sorted order of items of the array. It works faster than a linear search
algorithm. The binary search uses the divide and conquers principle.
Consider an array arr[ ] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
First Step:
Calculate the mid and compare the mid element with the key. If the key is less than mid
element, move to left and if it is greater than the mid then move search space to the right.
Key (i.e., 23) is greater than current mid element (i.e., 16). The search space moves to the
right.
16
Key is less than the current mid 56. The search space moves to the left.
Second Step:
If the key matches the value of the mid element, the element is found and stop search.
17
▪ Algorithm of Binary search/Interval search
18
▪ Flowchart of Binary search/Interval search
19
▪ C program for binary search: -
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
20
▪ Output: -
21
▪ Advantages of Binary Search
1. Binary search is faster than linear search, especially for large arrays.
2. More efficient than other searching algorithms with a similar time complexity, such as
interpolation search or exponential search.
3. Binary search is well-suited for searching large datasets that are stored in external memory,
such as on a hard drive or in the cloud
1. Binary search can be used as a building block for more complex algorithms used in machine
learning, such as algorithms for training neural networks or finding the optimal hyper
parameters for a model.
2. It can be used for searching in computer graphics such as algorithms for ray tracing or texture
mapping.
3. It can be used for searching a database
22
JSPM’s
JAYAWANTRAO SAWANT POLYTECHNIC,
Handewadi Road, Hadapsar, Pune-28
Department of Computer Engineering
Academic Year 2023-24
1 Laptop/Computer Windows 11 1
Processor 8GB RAM
3 Other - -
23
Teacher Evaluation Sheet
Name of students: Akhilesh Gawade, Aryan Panchal, Abhijeet Pawar, Anisha Mane
Enrollment No: 2207110609, 2207110610, 2207110611, 2207110612
Name of program: Computer Engineering
Semester: 3rd
Course Title: DSU
Code: 22317
Title of Micro Project: Searching Methods.
Course Outcomes Achieved:
3 Project Proposal
7 Report preparation
8 Presentation
9 Defense
24
Micro Project Evaluation Sheet
Proposal Model 10
(2 Marks) (4 Marks)
(2 Marks) (2 Marks)
Note: Every course teacher is expected to assign marks for group evaluation in first 3
columns and individual evaluation 4th column
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
……………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
…………………………………………………………………………………………………
……………………………………………………………
Signature: ………………………….
25