0% found this document useful (0 votes)
25 views11 pages

Unit-5 Searching & Sorting Techniques Not Completed

The document provides an overview of searching algorithms, emphasizing their importance in locating specific elements within data structures like arrays and lists. It details various types of searching methods, including linear and binary search, explaining their algorithms, complexities, and when to use them. Additionally, it compares linear and binary searching based on factors like efficiency, implementation, and data requirements.

Uploaded by

21230873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Unit-5 Searching & Sorting Techniques Not Completed

The document provides an overview of searching algorithms, emphasizing their importance in locating specific elements within data structures like arrays and lists. It details various types of searching methods, including linear and binary search, explaining their algorithms, complexities, and when to use them. Additionally, it compares linear and binary searching based on factors like efficiency, implementation, and data requirements.

Uploaded by

21230873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT-5 [SEARCHING & SORTING]

TOPIC-1 [INTRODUCTION TO SEARCHING]

Searching is the fundamental process of locating a specific element or item


within a collection of data. This collection of data can be arrays, lists, trees,
or other structured representations. Data structures are complex systems
designed to organize vast amounts of information. Searching within a data
structure is one of the most critical operations performed on stored data.
The goal is to find the desired information with its precise location quickly
and with minimal computational resources. It plays an important role in
various computational tasks and real-world applications, including
information retrieval, data analysis, decision-making processes, etc.

Characteristics of Searching
• Target Element/Key: It is the element or item that you want to find
within the data collection. This target could be a value, a record, a key,
or any other data entity of interest.
• Search Space: It refers to the entire collection of data within which
you are looking for the target element. Depending on the data
structure used, the search space may vary in size and organization.
• Complexity: Searching can have different levels of complexity
depending on the data structure and the algorithm used. The
complexity is often measured in terms of time and space
requirements.
• Deterministic vs. Non-deterministic: The algorithms that follow a
clear, systematic approach, like binary search, are deterministic.
Others, such as linear search, are non-deterministic, as they may need
to examine the entire search space in the worst case.
TOPIC-2 [TYPES OF SEARCHING]

1. Linear search: This is the simplest searching algorithm in the data


structures that checks each element of the data structure until the
desired element is found.
2. Binary search: This algorithm is used for searching in a sorted array
or list. It works by repeatedly dividing the search interval in half until
the desired element is found or the search interval is empty.
3. Interpolation Search: Like binary search, interpolation search works
on sorted lists. Instead of always dividing the search range in half,
interpolation search uses the value of the target element and the
values of the endpoints to estimate its approximate position within the
list. This estimation helps in quickly narrowing down the search space.
4. Fibonacci Search: It uses Fibonacci numbers to divide the search
space. It works on sorted arrays and has a similar approach to binary
search, but instead of dividing the array into halves, it divides it into
two parts using Fibonacci numbers as indices.
5. Exponential Search: It is a searching algorithm designed to find a
target value in a sorted collection, such as an array or list. It combines
elements of Binary Search and Linear Search to efficiently locate the
target, especially when its position is near the beginning of the
collection.
6. Ternary Search: It divides the search space into three parts instead
of two based on two splitting points. It is a divide-and-
conquer approach that operates on sorted lists.
7. Jump Search: Jump Search can be used on sorted collections (arrays
or lists). The idea is to reduce the number of comparisons by jumping
ahead by fixed steps or skipping some elements in place of searching
for all elements.

TOPIC-3 [LINEAR SEARCHING]


Linear Search, also known as Sequential Search, is one of the simplest and
most straightforward searching algorithms. It works by sequentially
examining each element in a collection of data(array or list) until a match is
found or the entire collection has been traversed.

For Example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90,
40} and key = 30

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.

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.

Algorithm for Linear Search:


C program for Linear Search:
Complexity Analysis of Linear Search:

• Time Complexity:

• Best Case: In the best case, the key might be present at the first
index. So the best case complexity is O(1)

• Worst Case: In the worst case, the key might be present at the
last index i.e., opposite to the end from which the search has
started in the list. So the worst-case complexity is O(N) where N
is the size of the list.

• Average Case: O(N)

• Auxiliary Space: O(1) as except for the variable to iterate through the
list, no other variable is used.

When to use Linear Search:

• When there is small collection of data.

• When data is unsorted.

TOPIC-4 [BINARY SEARCHING]


Binary Search is defined as a searching algorithm used in a sorted array by
repeatedly dividing the search interval in half. The idea of binary search is to
use the information that the array is sorted and reduce the time complexity
to O(log N).
For Example: Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91},
and the target = 23.
• Divide the search space into two halves by finding the middle index
“mid”.
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.

• 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.
Algorithm for Binary Search:

Complexity Analysis of Linear Search:


C program for Binary Search:
TOPIC-4 [DIFFERENCE BETWEEN LINEAR & BINARY
SEARCHING]
BASIS OF LINEAR SEARCH BINARY SEARCH
COMPARISON

DEFINITION The linear search starts It finds the position of the


searching from the first element searched element by finding
and compares each element the middle element of the
with a searched element till the array.
element is not found.

SORTED DATA In a linear search, the elements The pre-condition for the
don't need to be arranged in binary search is that the
sorted order. elements must be arranged
in a sorted order.

IMPLEMENTATION The linear search can be The implementation of


implemented on any linear data binary search is limited as it
structure such as an array, can be implemented only on
linked list, etc. those data structures that
have two-way traversal.

APPROACH It is based on the sequential It is based on the divide and


approach. conquer approach.

SIZE It is preferrable for the small- It is preferrable for the


sized data sets. large-size data sets.

EFFICIENCY It is less efficient in the case of It is more efficient in the


large-size data sets. case of large-size data sets.
WORST-CASE In a linear search, the worst- In a binary search, the
SCENARIO case scenario for finding the worst-case scenario for
element is O(n). finding the element is
O(log2n).

BEST-CASE In a linear search, the best-case In a binary search, the best-


SCENARIO scenario for finding the first case scenario for finding the
element in the list is O(1). first element in the list is
O(1).

DIMENSIONAL It can be implemented on both a It can be implemented only


ARRAY single and multidimensional on a multidimensional
array. array.

You might also like