📅 Date: Jan 12, 2026
🔥 Topic: Basic Array Operation: Linear Search
🔍 What is Linear Search?
Today I wrote my first algorithm. The goal: Find if a specific number (key) exists in the array.
Logic: Check every element one by one. If it matches, Bingo! If the loop ends and we found nothing, the number isn't there.
💻 Day 12 Code: Search Engine V1
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 50, 30, 70, 80, 20};
int size = 6;
int key;
bool found = false;
cout << "Enter number to search: ";
cin >> key;
for(int i = 0; i < size; i++) {
if(arr[i] == key) {
cout << "Found at Index: " << i << endl;
found = true;
break; // Stop searching once found
}
}
if(!found) {
cout << "Not Found!" << endl;
}
return 0;
}
💭 Thoughts
This is called O(n) complexity because in the worst case, I have to check every single number. Simple, but effective for small data.
No comments:
Post a Comment