Best case
This will arise when our key matches with the key at mid.So we will need only 1 comparison.
Time complexity = O(1)
Worst case
This case will arise when our key is not present in the array.In this case, the number of comparisons would be the times n can be divided by 2 to get 1.
Let n = no of array elements , c = no of comparisons required
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtoTDjhfIi8D5Tdw08U5nXuE-tNIgy4sXiKWp2_27Ms1b0Xr6v9eeY6ct0IlWsDCtJlPLRdFIEjUCaXYz-4UvbiOHVE5vaqJZAsYLOj5kDgLGWlupx5r2dZnhqmPEs0g1x-ThYtELVyBY/s320/bin_time1.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhddTJye6v6_XQ4IE7tp1sKrYr9mdpvHmYa94P9SyrAYw6J-RrM18udWk3GQTLKWMk2-IotPOaBLH96-pIzKhDHxOwH80NixNGJ-FfnpYYZs3l7Osshk1VM9ayjjKbW1Hzw9C97qtUUrZo/s400/bin_time.png)
Time Complexity = O(log n)
Lets take an example to verify the comparisons required.Here n = 8
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiva0hJVLG_eqjp-v1xcLLif-alpiydDF-j4WV05THuUh7j2704-zaDo3IIJdzH4AK7f95_Qjlac6U3dBZNDomzYd3BezPUqTmyoopZyemBpHNCJMIWj3QfiNH9FlI3-E4hi-bhEYlI6FA/s1600/bin_search_time_ex.png)
No comments:
Post a Comment