Why is Binary Search a divide and conquer algorithm?

后端 未结 16 2337
醉话见心
醉话见心 2020-12-14 02:12

I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached y

16条回答
  •  不知归路
    2020-12-14 02:57

    In a divide and conquer strategy :

    1.Problem is divided into parts;

    2.Each of these parts is attacked/solved independently, by applying the algorithm at hand (mostly recursion is used for this purpose) ;

    3.And then the solutions of each partition/division and combined/merged together to arrive at the final solution to the problem as a whole (this comes under conquer)

    Example, Quick sort, merge sort.

    Basically, the binary search algorithm just divides its work space(input (ordered) array of size n) into half in each iteration. Therefore it is definitely deploying the divide strategy and as a result, the time complexity reduces down to O(lg n).So,this covers up the "divide" part of it.

    As can be noticed, the final solution is obtained from the last comparison made, that is, when we are left with only one element for comparison. Binary search does not merge or combine solution.

    In short, binary search divides the size of the problem (on which it has to work) into halves but doesn't find the solution in bits and pieces and hence no need of merging the solution occurs!

    I know it's a bit too lengthy but i hope it helps :)

    Also you can get some idea from : https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/running-time-of-binary-search

    Also i realised just now that this question was posted long back! My bad!

提交回复
热议问题