Dsu Microproject Final1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Sanjivani Rural Education Society’s

SANJIVANI K. B. P. POLYTECHNIC
DEPARTMENT OF COMPUTER TECHNOLOGY

CERTIFICATE
This is to certify that the project report entitled
“TYPES OF SEARCHING IN C”
Submitted By

[169]GAWALI SIDDHI BALASAHEB


[ 164]SONAWANE SAKSHI JANARDHAN
[174]SHINDE SAKSHI KAILAS
[173] BHENDEKAR PRIYANKA KADUBAL

Under our supervision and guidance for partial fulfillment of the requirement for
Diploma in Computer Technology affiliated to
Maharashtra State Board of Technical Education, Mumbai
For academic year
2022-2023

Prof.V.A.PARJANE Mr.G .N. Jorvekar


Sanjivani Rural Education Society’s

DEPARTMENT OF COMPUTER TECHNOLOGY

CERTIFICATE
This is to certify that the project report entitled

Department of computer technology

Maharashtra State Board of Technical Education, Mumbai

Sanjivani rural education society’s

SANJIVANI K.B.P POLYTECNIC

KOPARGOAN -423601 ,DIST:AHEMEDNAGAR

2022 -2023
ACKNOWLEDGMENT
We would take this opportunity to express our sincere thanks and gratitude to our Project Guide Prof.
V.A.Parjane Department of Computer Technology, Sanjivani K.B.P.Polytechnic, Kopargaon. For his vital
guidance and support in completing this project. Lots of thanks to the Head of Computer technology
Department Mr. G.N. Jorvekar for providing us with the best support we ever had. We like to express our
sincere gratitude to Mr. A. R. Mirikar, Principal, Sanjivani K. B. P. Polytechnic, Kopargaon for
providing a great platform to complete the project within the scheduled time. Last but not the least; we would
like to say thanks to our family and friends for their never-ending love, help, and support in so many ways
through all this time. A big thanks to all who have willingly helped us out with their ability.

Miss. Sonawane Sakshi Janardhan [164]

Miss. Gawail Siddhi Balasaheb [169]

Miss. Bhendekar Priyanka Kadbul [173]

Miss. Shinde Sakshi Kailas [174]


1. WHAT IS SEARCHING IN C?

2. TYPES OF SEARCHES

3. WHAT IS BINARY SEARCH?

4. APPLICATIONS OF BINARY SEARCH

5. PROGRAM & ALOGRITHM

6. WHAT IS LINEAR SEARCH?

7. APPLICATIONS OF LINEAR SEARCH

8. PROGRAM & ALGORITHM

9. SOURCES

10. CONCLUSION

INDEX
WHAT IS SEARCHING IN C ?
Searching in data structure refers to the process of finding location LOC of an element in a list. This is
one of the important parts of many data structures algorithms, as one operation can be performed on an
element if and only if we find it. Various algorithms have been defined to find whether an element is
present in the collection of items or not. This algorithm can be executed on both internal as well as
external data structures.

The efficiency of searching an element increases the efficiency of any algorithm.


Searching is the process of finding some particular element in the list. If the element is present
in the list, then the process is called successful, and the process returns the location of that
element; otherwise, the search is called unsuccessful.

• Searching is the process of finding a given value position in a list of values.


• It decides whether a search key is present in the data or not.
• It is the algorithmic process of finding a particular item in a collection of items.
• It can be done on internal data structure or on external data structure.

In computer science, a search algorithm is an algorithm designed to solve a search problem. Search
algorithms work to retrieve information stored within particular data structure, or calculated in the search
space of a problem domain, with either discrete or continuous values. algorithms are

Visual representation of a hash table, a data structure that allows for fast retrieval of information.
Although search engines use search algorithms, they belong to the study of information retrieval, not
algorithmics.

The appropriate search algorithm often depends on the data structure being searched, and may also
include prior knowledge about the data. Search algorithms can be made faster or more efficient by
specially constructed database structures, such as search trees, hash maps, and database indexes.[1][full
citation needed][2]

TYPES OF SEARCHING
1. BINARY SEARCH
2. LINEAR SEARCH
WHAT IS BINARY SEARCH?
Binary Search is used with sorted array or list. In binary search, we
follow the following steps:

1. We start by comparing the element to be searched with the


element in the middle of the list/array.

2. If we get a match, we return the index of the middle element.

3. If we do not get a match, we check whether the element to be


searched is less or greater than in value than the middle element.

4. If the element/number to be searched is greater in value than the


middle number, then we pick the elements on the right side of
the middle element(as the list/array is sorted, hence on the right,
we will have all the numbers greater than the middle number),
and start again from the step 1.

5. If the element/number to be searched is lesser in value than the


middle number, then we pick the elements on the left side of the
middle element, and start again from the step 1.

Binary Search is useful when there are large number of elements in an


array and they are sorted.

So a necessary condition for Binary search to work is that the list/array


should be sorted.

Features of Binary Search


1. It is great to search through large sorted arrays.
2. It has a time complexity of O(log n) which is a very good time
complexity. We will discuss this in details in the Binary Search
tutorial.

3. It has a simple implementation.

his is a technique to search an element in the list using the divide and conquer

technique. This type of technique is used in the case of sorted lists. Instead of

searching an element one by one in the list, it directly goes to the middle

element of the list, divides the array into 2 parts, and decides element lies in

which sub-array the element exists.

Suppose ARR is an array with sorted n number of elements present in

increasing order. With every step of this algorithm, the searching is confined

within BEG and END, which are the beginning and ending index of sub-arrays.

The index MID defines the middle index of the array where,

MID = INT(beg + end )/2

It needs to be checked if ITEM < ARR[N} where ITEM is the element that we

need to search in ARR.


● If ITEM = ARR[MID] then LOC = MID and exit .

● If ITEM < ARR[MID} then ITEM can appear in the left sub-array, then BEG

will be the same and END = MID -1 and repeat.

● If ITEM > ARR[MID] then ITEM can appear in the right subarray then BEG

= MID+1 and END will be the same and repeat.

After this MID is again calculated for

respective sub-arrays, if we didn’t findthe

ITEM, the algorithm returns -1 otherwise

LOC =APPLICATION
MID. OF BINARY SEARCH
➢ This algorithm is used to search element in a given sorted array with more efficiency.

➢ It could also be used for few other additional operations like- to find the smallest element in
the array or to find the largest element in the array.

➢ Debugging a linear piece of code

➢ Figuring out resource requirements for a large system


➢ Any sorted collection from any language library like Java, .NET, C++ STL etc use binary search to
find values. Hence the principles behind the implementations of the binary search should be
known.

➢ Semiconductor test programs used for measuring digital timing or analog levels make extensive
use of binary search.

➢ Binary search can be also used to find numerical solutions to an equation.

ALGORITHM

BSEARCH(ARR, LB, UB, ITEM, LOC) Here, ARR is a sorted list of elements, with

LB and UB are lower and upper bounds for the array. ITEM needs to be

searched in the array and algorithm returns location LOC, index at which ITEM

is present else return -1.

1. Set BEG = LB, END = UB and MID = INT([BEG+END]/2)

2. Repeat step 3 and 4 while BEG <= END and ARR[MID] != ITEM

3. IF ITEM< ARR[MID] then:

Set END = MID-1


Else:

Set BEG = MID+1

4. Set MID = INT(BEG+END)/2

5. IF ARR[MID] = ITEM then:

Set LOC = MID

Else:

Set LOC = NULL

6. Exit.

Let’s say here, ITEM = 62

BEG = 1 and END =9 Hence MID = (1+9)/2 = 5

ARR[MID] = 52

Step 1: ARR[MID] < ITEM : thus END =9 and BEG = MID +1 = 6. Thus our new

sub-array is,
Step 2: Now BEG =6 and END =9 thus MID = INT([6+9]/2)= 6

NOW ARR[6] =ITEM. Thus LOC = MID

Thus LOC = 6

The complexity of Binary Search

Here are the complexities of the binary search given


below.

● Worst Case: O(nlogn)

● Best Case: O(1)

● Average Case: O(nlogn)


PROGRAM FOR BINARY SERACH
OUTPUT
WHAT IS LINEAR SERACH?
Linear search is a very basic and simple search algorithm. In Linear
search, we search an element or value in a given array by traversing
the array from the starting, till the desired element or value is found.

It compares the element to be searched with all the elements present


in the array and when the element is matched successfully, it returns
the index of the element in the array, else it return -1.

Linear Search is applied on unsorted or unordered lists, when there are


fewer elements in a list.

Linear search, often known as sequential search, is the most basic


search technique. In this type of search, you go through the entire list
and try to fetch a match for a single element. If you find a match, then
the address of the matching target element is returned.

On the other hand, if the element is not found, then it returns a NULL
value.

Features of Linear Search

1. It is used for unsorted and unordered small list of elements.

2. t has a time complexity of O(n), which means the time is linearly


dependent on the number of elements, which is not bad, but not
that good too.

3. It has a very simple implementation.


4. This is the traditional technique for searching an element in a collection

of elements. In this type of search, all the elements of the list are

traversed one by one to find if the element is present in the list or not.

One example of such an algorithm is a linear search. This is a

straightforward and basic algorithm. Suppose ARR is an array of n

elements, and we need to find location LOC of element ITEM in ARR. For

this, LOC is assigned to -1, which indicates that ITEM is not present in

ARR. While comparing ITEM with data at each ARR location, and once

ITEM == ARR[N], LOC is updated with location N+1. Hence we found

the ITEM in ARR.

APPLICATIONS OF LINEAR SERACH

The applications of linear equations are vast and are applicable in numerous real-
life situations. To handle real-life situations using algebra, we change the given
situation into mathematical statements. So that it clearly illustrates the relationship
between the unknown variables and the known information. The following are the
steps involved to reiterate a situation into a mathematical statement,
➢ Convert the real problem into a mathematical statement and frame it
in the form of an algebraic expression that clearly defines the problem
situation.
➢ Identify the unknowns in the situation and assign variables of these
unknown quantities.
➢ Read the situation clearly a number of times and cite the data,
phrases, and keywords. Sequentially organize the obtained
information.
➢ Write an equation using the algebraic expression and the provided
data in the statement and solve it using systematic equation solving
techniques
➢ Reframe the solution to the problem statement and analyze if it exactly
suits the problem.
Using these steps, the applications of word problems can be solved easily.

ALGORITHM

LSEARCH(ARR, N, ITEM, LOC) Here ARR Is the array of N number of

elements, ITEM holds the value we need to search in the array and

algorithm returns LOC, the location where ITEM is present in the ARR.

Initially, we have to set LOC = -1.


. Set LOC = -1,i=1

2. Repeat while DATA[i] != ITEM:

i=i+1

3. If i=N+1 ,then Set LOC =0

Else LOC = N+1

4. Exit.

Let’s say, below is the ARR with 10 elements. And we need to find whether

ITEM= 18 is present in this array or not.

In the start, LOC =-1

Step 1: ITEM != 77 thus we move to next element.

Step 2: ITEM != 56 thus we move to next element.


Step 3: ITEM != 14 thus we move to next element.

Step 4: ITEM != 7 thus we move to the next element.

Step 5: Hence ITEM == ARR[4] thus LOC updated to 5.

The complexity of Sequential Search

Here are the complexities of the


linear search given below.
Space
complexity
As linear search algorithm does not use any extra space, thus its space

complexity = O(n) for an array of n number of elements.

Time Complexity

● Worst-case complexity: O(n) – This case occurs when the search

element is not present in the array.

● Best case complexity: O(1) – This case occurs when the first element is

the element to be searched.

● Average complexity: O(n) – This means when an element is present

somewhere in the middle of the array.


PROGRAM FOR LINEAR SERACH
OUTPUT

Conclusion
Searching refers to finding the location of one element in the array of n

elements. There are 2 types of search linear and binary Search, Linear search

algorithm is straightforward and has O(n) of complexity whereas Binary Search


is a high-speed searching algorithm having the complexity of (logn) but can

only be used in case of the sorted list of elements. In case the size of the array

is large, it is preferable to use binary search instead of linear search. Binary

search is used in many searching data structures. In the case of mid-size

arrays, the linear search algorithm is more preferred.

Source

• Codeblock & VS code

• www.javatpoint.com

• www.geeksforgeeks.org

You might also like