Why is Binary Search a divide and conquer algorithm?

后端 未结 16 2297
醉话见心
醉话见心 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:50

    Divide and Conquer algorithm is based on 3 step as follows:

    1. Divide
    2. Conquer
    3. Combine

    Binary Search problem can be defined as finding x in the sorted array A[n]. According to this information:

    1. Divide: compare x with middle
    2. Conquer: Recurse in one sub array. (Finding x in this array)
    3. Combine: it is not necessary.

提交回复
热议问题