You are on page 1of 7

C++ implementation of Linear Search and Binary

Search

What is Linear Search?


Linear Search, also known as Sequential Search, is a straightforward method for finding
a particular element within a list. It sequentially checks each element in the list until a
match is found or the whole list has been searched.
 The time complexity of linear search algorithms is O(n), where n is the number of
elements in the list.
 It is used for smaller datasets and the data set is unsorted.

How Linear Search Works?


Use linear search algorithm in the given list: [2, 4, 6, 8, 10]. To find the number 6:
 Start from the first element (2).
 Compare the current element with the target element (6).
 If they match, the search ends.
 If they do not match, move to the next element.
 Repeat steps 2-4 until the target element is found or the list ends.

Advantage and Disadvantage of Linear Search


Aspect Linear Search

Simplicity: Linear Search is straightforward and easy to


Advantage implement, making it a good choice for small datasets and
simple search tasks.

Efficiency: Linear Search can be inefficient for large datasets


Disadvantage as it checks each element sequentially, leading to a higher time
complexity (O(n)) in worst-case scenarios.

What is Binary Search?


Binary Search is a more efficient searching algorithm that finds the position of a target
value within a sorted array. It compares the target value to the middle element of the
array and eliminates half of the search space successively.
 The time complexity of Binary Search is O(log n), making it more efficient for large
datasets.
 It is efficient for large datasets and requires sorted data.

How Binary Search Works?


Consider a sorted list of numbers: [2, 4, 6, 8, 10]. To find the number 6:
 Determine the middle element (4).
 Compare the middle element with the target element (6).
 Since 6 is greater than 4, eliminate the left half of the list.
 Repeat the process with the remaining half until the target element is found.

C++ Program Used to Measure Execution Time of Search Functions


This program generates random
data sets using #include <random>
library, and uses <chrono> library
for measuring execution time of
the search algorithms.

The choice of search algorithm can significantly affect performance for


different types of input data. Here's how the performance may vary:

1. Sorted Data:
 For sorted data, binary search performs much better than linear search.
 Binary search has a time complexity of O(log n), where n is the size of the
dataset. This means that its performance improves logarithmically as the
size of the dataset increases.
 Linear search, on the other hand, has a time complexity of O(n). Its
performance grows linearly with the size of the dataset. Therefore, for
large datasets, binary search will be much faster compared to linear
search.
2. Unsorted Data:
 In the case of unsorted data, binary search cannot be applied directly
because it requires the dataset to be sorted.
 Linear search is the only option for unsorted data. Its performance remains
O(n) regardless of the input data type.
 However, the performance of linear search may still vary based on the
distribution of the target element within the dataset. If the target element
is closer to the beginning of the dataset, linear search will find it faster. If
it's towards the end, it will take longer.
3. Nature of Data:
 For uniformly distributed data, there might not be a significant difference
in performance between linear and binary search, especially for small
datasets. This is because linear search may find the target element quickly
due to its uniform distribution.
 For skewed or irregularly distributed data, the performance of linear search
may vary greatly. If the target element is more likely to be found towards
one end of the dataset, linear search may take longer to find it.
4. Data Size:
 For very small datasets, the overhead of sorting the data (required for
binary search) may outweigh the benefits of the faster search time.
 As the dataset size grows larger, binary search becomes increasingly
advantageous over linear search due to its logarithmic time complexity.

Advantages and Disadvantages of Binary Search


Aspect Binary Search

Efficiency: Binary Search is highly efficient for large datasets. It


Advantage significantly reduces the search space by half with each comparison,
leading to a time complexity of O(log n).

Sorted Data Requirement: Binary Search requires the dataset to be sorted


Disadvantag
beforehand. This prerequisite can be a limitation if dealing with unsorted or
e
dynamically changing datasets.

Difference Between Linear and Binary Search: Linear vs Binary Search


Parameters Linear Search Binary Search

Linear Search sequentially checks Binary Search continuously divides the


Definition each element in the list until it finds a sorted list, comparing the middle element
match or exhausts the list. with the target value.

Time The time complexity is O(n), where n is The time complexity is O(log n), making it
Complexity the number of elements in the list. faster for larger datasets.

Less efficient, especially for large More efficient, especially for large
Efficiency
datasets. datasets.

Data
Does not require the list to be sorted. Requires the list to be sorted.
Requirement
Parameters Linear Search Binary Search

Implementatio
Easier to implement. Requires a more complex implementation.
n

Eliminates half of the search space with


Search Space Examines each element sequentially.
each comparison.

Suitable for small and unsorted


Use Case Ideal for large and sorted datasets.
datasets.

Key Differences Between Linear Search and Binary Search


 Binary Search is generally more efficient than Linear Search, especially for large
datasets.
 Linear Search has a time complexity of O(n), while Binary Search has a time
complexity of O(log n).
 Binary Search requires sorted data, whereas Linear Search does not.
 Binary Search is more complex to implement compared to Linear Search.
 Linear Search examines each element one by one whereas binary search eliminates
half of the search space with each step.

The time complexity of each algorithm:

Linear Search:

 Best Case: O(1) - Target element is found at the beginning of the array.
 Average Case: O(n) - Target element is found in the middle of the array.
 Worst Case: O(n) - Target element is found at the end of the array or not present
at all.
Binary Search:
 Best Case: O(1) - Target element is found at the middle of the array.
 Average Case: O(log n) - Target element is found in the middle of the array.
 Worst Case: O(log n) - Target element is not present in the array.
Graphical representation on the time complexity of both algorithm

Conclusions:
 The execution time of Linear Search increases linearly with the input size.
 The execution time of Binary Search increases logarithmically with the input size.
 Binary Search performs significantly better than Linear Search for larger input
sizes due to its logarithmic time complexity.
 However, Binary Search requires the dataset to be sorted, which may add an
overhead depending on the dataset's characteristics. Linear Search doesn't have
this requirement and can be used on unsorted data efficiently for smaller
datasets.
 In general, for large datasets and when the dataset is sorted, Binary Search is the
preferred choice due to its efficiency.

Scenarios where one search algorithm may be preferred


over another:
1. Sorted Data:
 Prefer binary search for sorted data due to its faster search time (O(log n)
compared to O(n) for linear search).
2. Unsorted Data:
 Linear search is the only option for unsorted data. It can be directly
applied without the need for sorting.
3. Small Datasets:
 For very small datasets, linear search may be preferred due to its simplicity
and lower overhead compared to sorting required for binary search.
4. Large Datasets:
 Binary search is advantageous for large datasets due to its logarithmic
time complexity, ensuring efficient search operations.
5. Memory Constraints:
 Linear search typically requires less memory overhead compared to binary
search, making it suitable for scenarios with memory constraints.
6. Real-time Applications:
 Binary search may be preferred in real-time applications where quick
response times are critical.
7. Ease of Implementation:
 Linear search is easier to implement and understand compared to binary
search, making it suitable for simpler applications or educational purposes.

You might also like