CodeGym /Courses /Python SELF EN /Comparison of Linear and Binary Search

Comparison of Linear and Binary Search

Python SELF EN
Level 53 , Lesson 3
Available

4.1 Comparing the Time Complexity of Linear and Binary Search

Let's compare the time complexity of linear and binary search.

Comparison of the time complexity of linear and binary search

Linear Search:

  • Time Complexity: O(n), where n is the number of elements in the array or list.
  • Best Case: O(1) — element found at the first position.
  • Average Case: O(n/2) = O(n) — element found somewhere in the middle on average.
  • Worst Case: O(n) — element found at the last position or not present.

Binary Search:

  • Time Complexity: O(log n), where n is the number of elements in the array or list.
  • Best Case: O(1) — element found on the first step (middle element).
  • Average Case: O(log n) — search in a sorted array.
  • Worst Case: O(log n) — element found on the last step or not present.

Example Analysis of Time Complexity

Linear Search:

  • Array [1, 3, 5, 7, 9, 11, 13], target element 7.
  • Check each element until finding 7 at index 3.
  • Took 4 checks, which corresponds to O(n).

Binary Search:

  • Array [1, 3, 5, 7, 9, 11, 13], target element 7.
  • Middle element (7) found on the first step.
  • Took 1 check, which corresponds to O(log n).

4.2 Advantages and Disadvantages of Each Method

Let's consider the pros and cons of each search type.

Linear Search:

Advantages:

  • Easy implementation: Linear search is very easy to implement and understand.
  • No data requirements: Linear search can be used on unsorted data.
  • Suitable for small arrays: Linear search is effective for small arrays.

Disadvantages:

  • Low efficiency for large arrays: The time complexity O(n) makes it inefficient for large arrays.
  • Long execution time: For large arrays, linear search can take a long time, especially if the target element is closer to the end of the array or not present.

Binary Search:

Advantages:

  • High efficiency for large arrays: The time complexity O(log n) makes it very efficient for large arrays.
  • Fast execution: Binary search is significantly faster than linear search when working with large sorted arrays.

Disadvantages:

  • Sorted data requirement: Binary search works only with sorted arrays, which may require additional time for initial sorting.
  • Complexity of implementation: Implementing binary search is more complex compared to linear search.

4.3 When to Use Which Search

Let's consider when to use linear search and when binary search.

Linear Search.

Use linear search when:

  • The array or list is unsorted.
  • The size of the array or list is small.
  • You need simplicity and a quick solution without extra costs for sorting.
  • It's required to find the first occurrence or all occurrences of an element.
  • Data is coming in real-time, and pre-sorting is impossible or impractical.

Binary Search.

Use binary search if:

  • The array or list is sorted.
  • The size of the array or list is large.
  • Frequent element searches in the same data set (the data can be pre-sorted once).
  • High search speed is important.
  • It's acceptable to spend time on pre-sorting the data.

4.4 Linear Search Example Problems

1. Searching in an Unsorted List

Need to find the index of a given number in an unsorted list of numbers.

Example:


def linear_search(arr, target):
    for index, element in enumerate(arr):
        if element == target:
            return index
    return -1

arr = [4, 2, 7, 1, 9, 3]
target = 7
print(linear_search(arr, target))  # Output: 2

2. Finding the First Occurrence in an Array

Need to find the first occurrence of a given element in a list of strings.

Example:


def linear_search(arr, target):
    for index, element in enumerate(arr):
        if element == target:
            return index
    return -1

words = ["apple", "banana", "cherry", "date", "banana"]
target = "banana"
print(linear_search(words, target))  # Output: 1

3. Searching in Real-Time Data

Find an element in a stream of real-time data.

Example:


import random

def find_in_stream(stream, target):
    for index, element in enumerate(stream):
        if element == target:
            return index
    return -1

stream = [random.randint(1, 100) for _ in range(100)]
target = 50
print(find_in_stream(stream, target))

4.5 Binary Search Example Problems

1. Searching in a Sorted Array

Need to find the index of a given number in a sorted array of numbers.

Example:


def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

sorted_array = [1, 3, 5, 7, 9, 11, 13]
target = 7
print(binary_search(sorted_array, target))  # Output: 3

2. Frequent Searching in a Large Dataset

Frequently search for elements in a large sorted array of numbers.

Example:


import random

sorted_large_array = sorted([random.randint(1, 1000000) for _ in range(1000000)])
target = random.choice(sorted_large_array)
print(binary_search(sorted_large_array, target))

3. Searching for an Element in a Sorted Database

Find a record in a sorted database by a key field.

Example:


database = sorted([{"id": i, "value": f"record_{i}"} for i in range(100000)])
def binary_search_db(db, target_id):
    left, right = 0, len(db) - 1
    while left <= right:
        mid = (left + right) // 2
        if db[mid]["id"] == target_id:
            return db[mid]
        elif db[mid]["id"] < target_id:
            left = mid + 1
        else:
            right = mid - 1
    return None

target_id = 54321
print(binary_search_db(database, target_id))
2
Task
Python SELF EN, level 53, lesson 3
Locked
Competition
Competition
2
Task
Python SELF EN, level 53, lesson 3
Locked
The Best Search
The Best Search
1
Survey/quiz
Search Algorithms, level 53, lesson 3
Unavailable
Search Algorithms
Search Algorithms
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION