logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Searching in Arrays

author
Generated by
Krishna Adithya Gaddam

06/12/2024

arrays

Sign in to read full article

Introduction

Searching is a crucial operation in computer science, especially when handling data within arrays. Whether you are trying to locate a specific value or verify its existence, efficient searching algorithms are vital. Today, we're going to explore two popular techniques for searching in arrays: Linear Search and Binary Search.


Linear Search

Linear Search, as the name suggests, is the most straightforward search algorithm. It works by checking each element in the array sequentially until it finds the target value or reaches the end of the array.

How Linear Search Works

  1. Start from the first element of the array.
  2. Compare the current element with the target value.
  3. If the current element matches the target, return the index of that element.
  4. If not, move to the next element and repeat steps 2-3 until the target is found or the end of the array is reached.

Example of Linear Search

Imagine you have an array of integers:

[4, 2, 7, 1, 9, 3]

You are looking for the number 3. The following steps illustrate how Linear Search would operate on this array:

  1. Compare 4 with 3 (no match)
  2. Compare 2 with 3 (no match)
  3. Compare 7 with 3 (no match)
  4. Compare 1 with 3 (no match)
  5. Compare 9 with 3 (no match)
  6. Compare 3 with 3 (match found at index 5)

Thus, the Linear Search will return 5, as the target 3 is located at index 5.

Time Complexity

  • Best Case: O(1) – occurs if the target is found at the first position.
  • Worst Case: O(n) – occurs if the target is not present or is at the last position.
  • Average Case: O(n) – since, on average, you'll check half the elements.

Binary Search

Binary Search is a more efficient algorithm than Linear Search, but it comes with a caveat: the array must be sorted beforehand. This method works by dividing the search interval in half repeatedly, eliminating half of the elements until the target value is found.

How Binary Search Works

  1. Start with two pointers representing the range: low and high.
  2. Calculate the midpoint: mid = (low + high) / 2.
  3. Compare the element at the midpoint with the target value.
    • If equal, return the index.
    • If the target is less, narrow the search to the left half (high = mid - 1).
    • If the target is greater, narrow the search to the right half (low = mid + 1).
  4. Repeat steps 2-3 until the target is found or the range is invalid.

Example of Binary Search

Consider the sorted array:

[1, 2, 3, 4, 7, 9]

You want to find the number 7. Here’s how Binary Search works on this array:

  1. Initial range: low = 0, high = 5
  2. Calculate mid = (0 + 5) / 2 = 2 (value at index 2 is 3).
  3. Since 7 is greater than 3, narrow to the right half: low = 3.
  4. New range: low = 3, high = 5; calculate mid = (3 + 5) / 2 = 4 (value at index 4 is 7).
  5. Since 7 equals 7, the target is found at index 4.

Time Complexity

  • Best Case: O(1) – if the target is at the midpoint.
  • Worst Case: O(log n) – as the algorithm halves the search area with each comparison.
  • Average Case: O(log n) – similar reasoning as the worst case.

Choosing the Right Search Method

When deciding whether to use Linear Search or Binary Search, consider the following:

  • Data Order: If your array is unsorted, you must use Linear Search, as Binary Search requires a sorted array.
  • Array Size: For small arrays, Linear Search can be faster due to its simplicity. However, as the size of the array increases, Binary Search becomes significantly more efficient.
  • Implementation Complexity: Binary Search requires additional logic for calculating midpoints and managing bounds, which can complicate implementation compared to Linear Search.

Both searching methods have their advantages, and knowing when to use each can greatly enhance your algorithmic skills!

Popular Tags

arrayslinear searchbinary search

Share now!

Like & Bookmark!

Related Collections

  • Trees Interview Questions Using Java

    13/10/2024 | DSA

  • Mastering Bit Manipulation: Unlocking Binary Power

    08/12/2024 | DSA

  • Advanced Dynamic Programming Interview Questions

    15/11/2024 | DSA

  • Advanced Graph Interview Questions in Java

    16/11/2024 | DSA

  • Advanced String-based Interview Techniques

    15/11/2024 | DSA

Related Articles

  • Mastering Backtracking Algorithms

    23/09/2024 | DSA

  • Understanding Array Operations

    06/12/2024 | DSA

  • Mastering the Merge Intervals Problem

    23/09/2024 | DSA

  • Understanding the Longest Common Subsequence Problem in Advanced Dynamic Programming

    15/11/2024 | DSA

  • Array Manipulation Techniques in Data Structures and Algorithms

    06/12/2024 | DSA

  • Reversing Words in a String

    15/11/2024 | DSA

  • Dynamic Programming Optimization

    03/09/2024 | DSA

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design