Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Introduction to Searching in Data Structures

data structure

Uploaded by

aryansabale7625
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Searching in Data Structures

data structure

Uploaded by

aryansabale7625
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Searching in Data Structures

Searching is a fundamental operation in computer science that involves finding a specific


element or a set of elements within a collection of data. As data grows in size and complexity,
efficient searching techniques become crucial for performance, especially in applications like
databases, search engines, and data analysis.

Searching algorithms can be broadly categorized into two types:

1. Unordered Search: This applies to data that is not sorted. The most common method
here is linear search, where each element is checked sequentially until the target is found
or the end of the list is reached. This approach is simple but can be inefficient for large
datasets, as its time complexity is O(n).
2. Ordered Search: This applies to sorted data, where more efficient algorithms can be
utilized. The most notable among these is binary search, which repeatedly divides the
search interval in half, significantly reducing the number of comparisons required. The
time complexity of binary search is O(log n), making it much faster than linear search for
large datasets
Linear search
Linear search, also known as sequential search, is one of the simplest searching algorithms. It
works by checking each element in a list or array one by one until the desired element is found or
the end of the collection is reached.

How linear search work

1. Start from the First Element: The algorithm begins at the first element of the array or list.
2. Compare: It compares the current element to the target value.
3. Found or Not: If the current element matches the target, the search is successful, and the index
of that element is returned. If not, it moves to the next element.
4. Repeat: This process continues until the target is found or the end of the collection is reached.

Time complexity

The time complexity of linear search is O(n)O(n)O(n), where nnn is the number of elements
in the list or array being searched. In the worst-case scenario, you may have to check every
element in the list to find the target value (or determine that it is not present). In the best case,
the target may be the first element, resulting in O(1)O(1)O(1) time. However, the average
case is still considered O(n)O(n)O(n).

Algorithm of linear search:

#include <stdio.h>

// Function to perform linear search


int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index of the target element
}
}
return -1; // Return -1 if the target is not found
}

int main() {
int arr[] = {5, 3, 8, 6, 2}; // Example array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate size of the array
int target = 6; // Element to search for

int result = linearSearch(arr, size, target);

if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found in the array.\n");
}

return 0;
}
Binary Search
Binary search is a fundamental algorithm used in data structures for efficiently finding an
element in a sorted collection. Here's a more focused look at binary search within the context of
data structures:

Definition: Binary search is an algorithm that finds the position of a target value within a sorted
array or list by repeatedly dividing the search interval in half.

Key Properties

1. Sorted Array Requirement:


o The array or list must be sorted in ascending or descending order for binary search
to work.
2. Efficiency:
o Time Complexity: O(log⁡n)O(\log n)O(logn)
o Space Complexity: O(1)O(1)O(1) for iterative implementation, O(log⁡n)O(\log
n)O(logn) for recursive implementation due to the call stack.

How Binary Search Works

1. Initialization:
o Set two pointers: left (start of the array) and right (end of the array).
2. Iteration:
o Calculate the mid index: mid=left+(right−left)2\text{mid} = \text{left} + \frac{(\
text{right} - \text{left})}{2}mid=left+2(right−left).
o Compare the target value with the value at mid:
 If equal, return mid.
 If the target is less than the value at mid, search in the left half by
adjusting right.
 If the target is greater, search in the right half by adjusting left.
3. Termination:
o The search continues until left exceeds right, indicating the target is not in the
array.

Algorithm of binary search:

#include <stdio.h>

// Function to perform binary search


int binarySearch(int arr[], int size, int target) {
int left = 0; // Starting index
int right = size - 1; // Ending index
while (left <= right) {
int mid = left + (right - left) / 2; // Calculate the middle index

// Check if the target is present at mid


if (arr[mid] == target) {
return mid; // Target found
}

// If target is greater, ignore the left half


if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore the right half
else {
right = mid - 1;
}
}
return -1; // Target not found
}

int main() {
int arr[] = {2, 3, 5, 6, 8}; // Example sorted array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate size of the array
int target = 6; // Element to search for

int result = binarySearch(arr, size, target);

if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found in the array.\n");
}

return 0;
}

.
Importance of Searching

 Performance: Efficient searching algorithms improve the performance of applications


that require quick data retrieval.
 Scalability: As datasets grow, choosing the right search algorithm can greatly affect
scalability and responsiveness.
 Application Diversity: Searching is used in various domains, from retrieving user
information in databases to locating resources in file systems and optimizing search
queries in search engines.

Summary
Feature Linear Search Binary Search
Data Structure Unsorted or sorted Sorted only
Time Complexity O(n) O(log n)
Space Complexity O(1) O(1) (iterative), O(log n) (recursive)
Implementation Simple More complex
Best Use Case Small/unsorted datasets Large/sorted datasets

 In summary, linear search is a straightforward method that can be used on any list, while
binary search is highly efficient but requires the data to be sorted. The choice between the
two depends on the specific requirements and constraints of your application.

Conclusion

Understanding different searching techniques is essential for software developers and data
scientists. By choosing the appropriate algorithm based on the nature of the data and the specific
requirements of the task, one can ensure optimal performance and efficiency in data handling
operations.

You might also like