πŸš€ Searching Algorithms in DSA – Linear Search & Binary Search


πŸ’‘ Did You Know?

Imagine finding a name in a list of 10,000 students…

πŸ‘‰ Checking one by one is slow ❌
πŸ‘‰ Using smart algorithms is faster ✅

That’s why Searching Algorithms are important in DSA πŸš€


πŸ”° Introduction

Searching means finding a specific element in data.

πŸ‘‰ Example:

  • Finding a contact in your phone πŸ“±

  • Searching a word in dictionary πŸ“–

  • Finding student record πŸ’»

There are two important searching methods:

  1. Linear Search

  2. Binary Search


πŸ”Ή 1. Linear Search

πŸ‘‰ Linear Search checks elements one by one from start to end.


πŸ–Ό️ Visualization




πŸ’» Linear Search Program

#include <stdio.h>   // Includes standard input/output functions like printf()

int main() {         // Main function where program execution starts

    int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize an array of 5 elements
    
    int key = 30;    // Store the element we want to search

    for(int i = 0; i < 5; i++) {   // Loop through all array elements

        if(arr[i] == key) {        // Check if current element matches key
            
            printf("Element found at index %d", i); // Print index of found element
            
            return 0;              // Exit program successfully
        }
    }

    printf("Element not found");   // Print if element is not found after loop ends

    return 0;                      // End program successfully
}

πŸ“€ Output

Element found at index 2

⚡ Time Complexity

πŸ‘‰ Best Case: O(1)
πŸ‘‰ Worst Case: O(n)


πŸ’‘ Real-Life Example

πŸ‘‰ Searching a contact manually in an unsorted list.


πŸ”Ή 2. Binary Search

πŸ‘‰ Binary Search is a faster searching technique.

⚠️ It works only on sorted arrays.


πŸ–Ό️ Visualization




πŸ’» Binary Search Program

#include <stdio.h>   // Includes standard input/output library

int main() {         // Main function starts execution

    int arr[7] = {10, 20, 30, 40, 50, 60, 70}; // Sorted array declaration

    int low = 0;     // Starting index of array
    int high = 6;    // Last index of array
    int mid;         // Variable to store middle index
    int key = 50;    // Element to search

    while(low <= high) {   // Continue searching while low <= high

        mid = (low + high) / 2;   // Calculate middle index

        if(arr[mid] == key) {     // Check if middle element equals key
            
            printf("Element found at index %d", mid); // Print index if found
            
            return 0;             // Exit program
        }

        else if(arr[mid] < key) { // Check if key is greater than mid element
            
            low = mid + 1;        // Move search to right half
        }

        else {                    // Otherwise key is smaller
            
            high = mid - 1;       // Move search to left half
        }
    }

    printf("Element not found");  // Execute if element not present

    return 0;                     // End program
}

πŸ“€ Output

Element found at index 4

⚡ Time Complexity

πŸ‘‰ Best Case: O(1)
πŸ‘‰ Worst Case: O(log n)


πŸ’Ž Linear Search vs Binary Search




⚠️ Common Mistakes

❌ Using Binary Search on unsorted arrays
❌ Wrong middle index calculation
❌ Incorrect loop conditions


πŸ’‘ Pro Tip

πŸ‘‰ Use:

  • Linear Search for small data

  • Binary Search for large sorted data


πŸ§ͺ Practice Questions (with Answers)

1. What is searching?

πŸ‘‰ Answer:
Searching means finding an element in data.


2. Which search works on unsorted arrays?

πŸ‘‰ Answer:
Linear Search.


3. Which search is faster?

πŸ‘‰ Answer:
Binary Search.


4. What is required for Binary Search?

πŸ‘‰ Answer:
Array must be sorted.


πŸ’Ž Final Words

Searching algorithms are one of the most important concepts in DSA.

πŸ‘‰ Once you master them:

  • Problem-solving improves

  • Logic becomes stronger

  • Coding becomes faster

πŸš€ Keep practicing and keep growing!


πŸ“’ Support & Share

πŸ’¬ If you found this helpful, share it with your friends!

πŸš€ Follow Learning Growth Hub for more powerful DSA tutorials.

πŸ“Œ Keep Learning. Keep Coding. Keep Growing.

Written by Krishna Popat
🌱 Founder – Learning Growth Hub


Comments

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”