⏳ টাইম কমপ্লেক্সিটি (Time Complexity)
আমরা যখন একটা algorithm লিখি, তখন সবচেয়ে important বিষয় হলো efficiency ⚡। Efficiency বোঝার জন্য আমরা Time Complexity analyze করি, যা বলে দেয় input size (n) এর ওপর ভিত্তি করে operations কেমনভাবে grow করবে 📈।
অনেকে মনে করে Time Complexity মানে কোড কত দ্রুত রান হবে ⏱️, কিন্তু আসলে এটা execution time না! কারণ CPU speed, RAM, OS ইত্যাদির ওপর নির্ভর করে exact time পরিবর্তন হতে পারে। তাই আমরা operations count করি 🔢।
📊 Time Complexity কিভাবে Measure করা হয়?
Time Complexity বোঝার জন্য আমরা দেখি input size (n) বাড়লে operations কেমন বাড়ছে। 🧐
Example 1️⃣: O(n)
for(int i = 0; i < n; i++) {
cout << "Hello" << endl;
}
✅ Explanation:
- n = 10 হলে → 10 বার Hello print হবে 🖨️
- n = 1,000 হলে → 1,000 বার Hello print হবে 🔁
- অর্থাৎ operations = n, তাই Time Complexity = O(n)
Example 2️⃣: O(n²)
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << "Hello" << endl;
}
}
✅ Explanation:
- প্রথম লুপ n বার চলবে 🔄
- দ্বিতীয় লুপ n বার চলবে 🔄
- মোট n × n = n² বার Hello print হবে 🖨️
- তাই Time Complexity = O(n²) 📈
🚀 Asymptotic Notation (Big O, Theta, Omega)
Time Complexity বোঝানোর জন্য Asymptotic Notation use করা হয়, যা তিন ধরনের 🏷️:
1️⃣ Big O (O) — Worst Case 😱
👉 Worst case scenario বোঝায়, যখন কোড সবচেয়ে slow হয়।
👉 Example: Linear Search — যদি last element খুঁজতে হয়, তখন O(n) time লাগবে.
2️⃣ Theta (Θ) — Average Case 😐
👉 Average scenario বোঝায়, যেখানে কোড সাধারণত medium speed এ রান করবে।
👉 Example: Finding a random element in an unsorted list.
3️⃣ Omega (Ω) — Best Case 😍
👉 Best case scenario বোঝায়, যখন কোড super fast হয়ে যায়।
👉 Example: Linear Search — যদি প্রথমেই element পাওয়া যায়, তখন O(1) time লাগবে।
🔥 কেন Time Complexity Important?
ধরো, দুইজন student একই problem solve করছে 📚:
✅ Student A:
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
Time Complexity = O(n) ⏳
✅ Student B:
int sum = (n * (n + 1)) / 2;
Time Complexity = O(1) ⚡
যদি n = 1,000,000 হয়, তাহলে Student A এর 1 million operations লাগবে 😵, কিন্তু Student B মাত্র 1 operation দিয়েই solve করবে 🤯।
👉 এই কারণেই আমরা Time Complexity analyze করি, যেন efficient algorithm লিখতে পারি! 🚀
📌 Common Time Complexities with Examples
1️⃣ O(1) — Constant Time ⚡
- Example: Accessing an element from an array →
arr[i]
- No matter how big the input is, it always takes constant time.
2️⃣ O(log n) — Logarithmic Time 🚀
- Example: Binary Search
- Every step, the input size reduces by half, making it very efficient.
3️⃣ O(n) — Linear Time 🔄
- Example: Linear Search
- If there are n elements, it takes n steps in the worst case.
4️⃣ O(n log n) — Log-Linear Time ✅
- Example: Merge Sort, Quick Sort
- Faster than O(n²) sorting algorithms, used in efficient sorting techniques.
5️⃣ O(n²) — Quadratic Time 🐢
- Example: Nested loops (Bubble Sort, Selection Sort)
- If n = 1000, it may take 1,000,000 operations!
6️⃣ O(2ⁿ) — Exponential Time 🛑
- Example: Recursive Fibonacci Calculation
- Very inefficient, doubles the work at each step.
7️⃣ O(n!) — Factorial Time 🚫
- Example: Traveling Salesman Problem (TSP)
- Extremely slow, only works for very small inputs.
👉 Remember: Lower complexity = Faster code! 🔥
🎯 Final Thought
👉 Efficient algorithm design এর জন্য Time Complexity জানা খুব important!
👉 Best case, worst case, average case বুঝতে হবে।
👉 Big O notation use করে efficiency compare করা যায়।
👉 Nested loops মানেই high complexity, যত কম loop তত ভালো!
Hope this helps! 😃🚀 If you have any questions, feel free to ask! 💡