Best-Worst-and-Average-case-Analysis
- Linear search
- binary search tree
we have a array A:
8 | 6 | 12 | 5 | 9 | 7 | 4 | 3 | 16 | 18 |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Linear Search
key = 7, means we need search 6 times key = 20, means we need find 10 times
Best Cases
Best case: searching key element presented at first index Best case Time: , so
Worst Cases
Worst case: searching key element presented at last index Worst case Time: , so
Average Cases
Average case:
, , ,
, , ,
note: best cases and worst cases can use any symbol to represent
- using to represent bad cases
- using to represent best cases
Binary Search
key = 49 means we need search 3 times height = log2n = log2 8 = 3