π 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:
Linear Search
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
Post a Comment