Big O(n) Notation π€π
Big O(n) ka matlab hai “linear time”. Iska matlab hai ki jaise-jaise input size (n) badhta hai, waise-waise algorithm ka execution time bhi directly proportional tarike se badhta hai.
Agar input size double hota hai, toh execution time bhi double hoga. Agar input size 10x hota hai, toh time bhi 10x badhega.
Easy Explanation with Example π
Socho tum supermarket gaye ho aur tumhe apna favorite item search karna hai:
- Agar supermarket ke shelves kam hai, tum jaldi search kar loge.
- Agar shelves zyada hai, tumhe utna hi zyada time lagega sab dekhne me.
Input size (shelves) ke saath search ka time directly badhta hai. Yeh hota hai O(n). π
Example in Code π»
function findItem(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i; // Item mil gaya, index return karo
}
}
return -1; // Item nahi mila
}
Yahaan algorithm ko worst-case mein poore array ke elements check karne padenge. Agar array size bada hoga, toh time bhi proportionally badhega. Iska Big O hai O(n). π
Daily Life Examples π§Ί
List Mein Naam Dhoondhna (Search Karna): Socho tumhare paas 100 logon ki list hai aur tumhe “Reyansh” ka naam dhoondhna hai. Tum ek-ek naam check karte jaoge. Jitne zyada log, utna zyada time lagega. O(n).
Shopping Cart ka Total Calculate Karna: Tumhare cart mein 5 items hain toh tumhe 5 prices ko add karna padega. Agar 50 items hain, toh 50 prices ko add karna hoga. Items badhne par time bhi badhega. O(n).
Parks Mein Apna Dost Dhoondhna: Tum ek park ke har kone mein jaake check karte ho ki dost kahan hai. Agar park chhota hai, jaldi ho jayega. Agar park bada hai, toh zyada time lagega. O(n). π️
Key Points to Remember π
- O(n) ka matlab: Time input size ke directly proportional hai.
- Performance: Efficient hai, but O(1) se slow hai.
- Real Life: Har wo task jo ek-ek element process kare, mostly O(n) hoga.
⏳ Jaise input badega, waise kaam ka time bhi badega.
Comments
Post a Comment