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).