Time Complexity of an algorithm
Time Complexity ek tarika hai jisse hum yeh samajhte hain ki koi algorithm bada input (large input size N) milne par kitna time lega.
- Yeh time ko seconds me measure nahi karta.
- Yeh sirf fundamental operations (basic steps) count karta hai jo algorithm input size N ke hisab se perform karti hai.
- Fundamental Operations me aate hain:
- Assignment (value dena)
- Comparison (tulna karna)
- Arithmetic operations (plus, minus, multiply)
Kyun Zaroori Hai?
Time complexity batati hai ki algorithm kitni efficient hai. Jab input size N bahut badhta hai, toh hum dekhte hain ki algorithm ka execution time kis speed se badh raha hai.
Time Complexity Ko Kaise Denote Karte Hain? (Asymptotic Notations)
Time complexity ko 3 popular notations se express kiya jata hai, jinme Big O sabse zyada use hota hai.
1. Big O Notation (O): Upper Bound
- Isko Worst-Case Time Complexity bhi bolte hain.
- Yeh batata hai ki algorithm worst situation me kitna time legi.
- Yeh guarantee karta hai ki algorithm ka time is limit se aage nahi badhega.
- Example: Agar complexity O(N²) hai, matlab time N² se tez nahi badhega.
2. Omega Notation (Ω): Lower Bound
- Isko Best-Case Time Complexity bolte hain.
- Yeh batata hai ki algorithm kam se kam kitna time leg sakti hai.
3. Theta Notation (Θ): Tight Bound
- Yeh batata hai ki actual time best aur worst ke beech me hota hai.
- Accurate representation deta hai.
Common Time Complexities (Aam Time Complexities)
Algorithm design me kuch common time complexities hoti hain jinka use zyada hota hai.
| Notation | Name | Growth with N | Example |
|---|---|---|---|
| O(1) | Constant Time | Same rehta hai (input se farak nahi) | Array index se element nikalna |
| O(log N) | Logarithmic Time | Bahut dheere badhta hai | Binary Search |
| O(N) | Linear Time | Seedha badhta hai (input ke barabar) | Array print karna |
| O(N log N) | Linearithmic Time | Tezi se badhta hai | Merge Sort, Heap Sort |
| O(N²) | Quadratic Time | Bahut tezi se badhta hai | Bubble Sort, Selection Sort |
| O(2ⁿ) | Exponential Time | Extreme growth (avoid karo) | TSP (Brute Force) |
Example: O(N) vs O(N²)
Maan lo N = 1000:
- O(N): Algorithm ~1000 operations karegi (fast).
- O(N²): Algorithm ~10,00,000 (1 million) operations karegi (slow).
Comments
Post a Comment