0% found this document useful (0 votes)
2 views2 pages

Complexity

The document discusses algorithmic complexities for various search and sorting methods, comparing Binary Search Trees (BST) and insertion sort. It highlights the efficiency of BST for later insertions and provides a breakdown of the worst, average, and best-case scenarios for different algorithms. The document concludes with a clarification on calculating time complexity, emphasizing the importance of understanding the relationship between outer and inner loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Complexity

The document discusses algorithmic complexities for various search and sorting methods, comparing Binary Search Trees (BST) and insertion sort. It highlights the efficiency of BST for later insertions and provides a breakdown of the worst, average, and best-case scenarios for different algorithms. The document concludes with a clarification on calculating time complexity, emphasizing the importance of understanding the relationship between outer and inner loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Thats pg 168, so we have to consider what the task is and then see what algorithm is more

appropriate.

The point:
1.​ Original set up: O(N*log(N)) for both (building BST and sorting an originally unsorted
array)
2.​ Later insertions: O(log(N)) for BST vs O(N) for insertion sort in array of O(N*log(N)) for
resorting (both worse than BST).

Algorithm Worst Average Best

Binary Search O(log(N))

Linear Search O(N)

Bubble Sort O(N^2)

BST Search O(N) O(log(N))

BST Insert O(N) O(log(N))

BST Delete O(N) O(log(N):


For cases a,b its one
O(log(N)) search. Fo
case c its one extra
O(log(N)) search

BST traversal O(N)


The way to solve is not to muriply the times the outer loop runs by the times the inner one runs.
This wont work. The outer loop will tell us how much times the inner loop will run but its
independent of the big O calculation.

Solution:​

Out runs n times and i increases by one each time. So this results in the inner loop running
1+2+3+4+....+(n-1)= n(n+1)/2= O(n^2).

You might also like