Time Complexity in DSA In Hindi

Other Blogs

Blogs ❯❯ DSA

Image could not load

Time Complexity In DSA by Agê Barros on Unsplash

अगर आप coding या DSA (Data Structures and Algorithms) सीख रहे हो, तो आपको Time Complexity समझना जरूरी है। ये बताता है कि कोई algorithm कितनी जल्दी या धीरे काम करेगा, जैसे जैसे input size बढ़ता है।

इस blog में हम समझेंगे Time Complexity के basic types using JavaScript examples, और देखेंगे कौनसा fast है और कौनसा slow.

🧠 Basic Concept Of Time Complexity

Time Complexity बताता है कि आपके algorithm कि performance input size के respect में कैसे change होती है, ये usually O notation में लिखा जाता है, जैसे:

  • O(1) → Constant Time

  • O(n) → Linear Time

  • O(n²) → Quadratic Time

  • O(log n) → Logarithmic Time

  • O(n log n), O(2ⁿ), O(n!) → और ज़्यादा complex

Types of Time Complexities

1. O(1) – Constant Time

अगर कोई operation input size से affect नहीं होता, तो वो constant time होता है।

// JavaScript function getFirstElement(arr) { return arr[0]; }

In above example, चाहेarray में 1 item हो या 10,000 — answer एक ही step में मिलता है।

2. O(n) – Linear Time

अगर आप हर element को एक बार process कर रहे हो, तो वो linear time है।

function printAll(arr) { arr.forEach(item => console.log(item)); }

यहां , जैसे जैसे array बड़ा होगा, बैसे - बैसे time भी बढ़ेगा।

Used In:

  • Linear Search

  • Looping through arrays, lists, or strings

  • Finding max/min in an unsorted array

  • Basic file reading (line-by-line)

3. O(n²) – Quadratic Time

अगर आपको nested loop लगानी पड़ती है, तो mostly complexity होती है।

for(let i=0; i<n; i++) { for(let j=0; j<n; j++) { console.log(i, j); } }

यहां पर अगर Input size 2 तो steps 4 होंगे और input size 3 तो steps 9 हो जायेंगे। क्योंकि outer loop के हर iteration पर , inner loop n time run होगा।

Used In :

  • Brute-force algorithms

  • Bubble Sort, Insertion Sort, Selection Sort

  • Checking all pairs (2 nested loops)

  • Graphs: checking all edges for each node (dense graph)

4. O(log n) – Logarithmic Time

Binary Search जैसी techniques में input हर बार half हो जाता है।

function binarySearch(arr, target) { let start = 0, end = arr.length - 1; while (start <= end) { let mid = Math.floor((start + end) / 2); if (arr[mid] === target) return mid; else if (arr[mid] < target) start = mid + 1; else end = mid - 1; } return -1; }

ये बहुत efficient है, large data के लिए best .

Used In:

  • Binary Search

  • Operations on Balanced Binary Search Trees (AVL, Red-Black Trees)

  • Heap insert/delete

  • Binary Search on answer (in problems like Min-Max optimization)

5. O(n log n) – Merge Sort / Efficient Sorting

Sorting algorithms जैसे Merge Sort या Quick Sort average case में यही time लेते हैं।

📌 थोड़ा complex, but efficient sorting के लिए काफी use होता है.

Used In:

  • Merge Sort

  • Quick Sort (average case)

  • Heap Sort

  • Efficient divide-and-conquer strategies

  • Some greedy & dynamic programming problems (like building Huffman Tree)

⚠️ 6. O(2ⁿ) – Exponential Time

ऐसे algorithms जहाँ हर step पर 2 new choices बनती हैं।

function fib(n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); }

⚠️ 7. O(n!) – Factorial Time

All permutations जैसी problems में हर possible order check करना पड़ता है।

📌 जैसे Travelling Salesman Problem, या N-Queens brute force.

🧠 Conclusion : Which one is best

  • Fastest: O(1) > O(log n) > O(n) > O(n log n)

  • Avoid: O(n²), O(2^n), O(n!)

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !