English 中文(简体)
为什么是平常搜索的鸿沟和 algorithm算法?
原标题:Why is Binary Search a divide and conquer algorithm?

有人问我,在一次考试中,笔试是分门别类的算术。 我的回答是肯定的,因为你把这个问题分为小小小小小小小小小小小小小小小小问题,直到你取得结果。

但是,考试者问,考试的一部分是什么,我无法回答。 他们还不赞成它实际上是一种分裂和 algorithm算法。

但是,在任何地方,我都说,这是,因此,我很想知道,为什么,以及其中哪一部分是?

最佳回答

该书:

Data Structures and Algorithm Analysis in Java (2nd Edition), by Mark Allen Weiss

Says that a D&Cetic should have two disjoint recursive calls,一如QuickSort。

本搜索网站没有这种,尽管可以重新实施。

问题回答

我认为这并不是分裂和征服,见以下段落:http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm>http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm

recursively breaking down a problem into two or more sub-problems which are then combined to give a solution

在双轨搜索中,只有一个问题只是每步减少一半数据,因此不需要结果的(合并)阶段。

页: 1

为补充@Kenci s post,DnC算法有少数一般/共同财产:

  1. divide the original problem instance into a set of smaller sub-instances of itself;
  2. independently solve each sub-instance;
  3. combine smaller/independent sub-instance solutions to build a single solution for the larger/original instance

宾塔搜索的问题是,它确实会产生一套独立的次级需要,按照步骤1加以解决;它只是简化<>>。 页: 1 换言之,它只会缩小问题的规模,而且从现在起就减少问题。

DnC算法不仅应确定/解决原始问题较小的分属问题,而且应使用一套部分独立的解决方案,“建立”整个大问题单一解决办法。

The book Fundamentals of Algorithmics , G. Brassard, P. Bratley 下面(我强调,斜体为原文):

这可能是最简单地应用鸿沟和数量,事实上,严格地说,这是指适用plification,而不是分界线:对于任何足够大的事例,如果是半个小国,解决办法就会缩小到一个小小小小小小小小的国家。

Section 7.3 Binary Search on p.226.

在分裂和qu的战略中:

1. 混合物分为部分;

2. 每一部分都通过适用现行算法(主要是为此目的进行再保险)受到独立攻击/解散;

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)

例如,Quick,合并。

基本上,双轨搜索算法仅将其工作空间(按数量(顺序排列)的体积分为零。 因此,这无疑是部署“divide战略”,因此时间复杂性降低到O(lg n)。 因此,这涵盖了其中的“divide”部分。

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.

简言之,双轨搜索将problem(必须工作)的大小分为二分之四,但在借方和碎块中却未找到<>solution<>>,因此无需将解决办法合并!

我知道它太长了,但希望它有助于:

也可从以下网站获得一些想法:。 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!

Apparently some people consider binary search a divide-and-conquer algorithm, and some are not. I quickly googled three references (all seem related to academia) that call it a D&C algorithm: http://www.cs.berkeley.edu/~vazirani/algorithms/chap2.pdf http://homepages.ius.edu/rwisman/C455/html/notes/Chapter2/DivConq.htm http://www.csc.liv.ac.uk/~ped/teachadmin/algor/d_and_c.html

我认为,大家一致认为,D&C算法至少应分这三步的前两个阶段:

  • divide, i.e. decide how the whole problem is separated into sub-problems;
  • conquer, i.e. solve each of the sub-problems independently;
  • [optionally] combine, i.e. merge the results of independent computations together.

第二阶段——连续阶段——应重新采用同样的方法,通过分成小小小小小小小小小小子问题等,解决小问题。 然而,在实践中,往往使用一些门槛来限制休养的做法,因为对于小面积问题,不同的做法可能更快。 例如,在阵列面积小时,快速执行往往使用泡沫。

第三阶段可能不可行,我认为它不会将算法定为D&C。 一个常见的实例是,将“> 的<代码>重新定位,即与纯粹与独立数据项目(即不减少任何形式)合作的所有迭代。 它可能无用地看着眼光,但事实上,它非常有力,例如平行地实施 lo,并且利用Cilk和Intels TBB等框架。

回到原来的问题:请考虑实施算法的一些法典(我使用C++;如果这不是为了你所喜欢的语言,则担心:

int search( int value, int* a, int begin, int end ) {
  // end is one past the last element, i.e. [begin, end) is a half-open interval.
  if (begin < end)
  {
    int m = (begin+end)/2;
    if (value==a[m])
      return m;
    else if (value<a[m])
      return search(value, a, begin, m);
    else
      return search(value, a, m+1, end);
  }
  else // begin>=end, i.e. no valid array to search
    return -1;
}

这里的分部分为<代码>int m = (begin+end)/2;,其余部分为序部分。 计算法以回馈式D&C形式明确写,尽管只采用了一个分行。 然而,也可以用假形式写:

int search( int value, int* a, int size ) {
  int begin=0, end=size;
  while( begin<end ) {
    int m = (begin+end)/2;
    if (value==a[m])
      return m;
    else if (value<a[m])
      end = m;
    else
      begin = m+1;
  }
  return -1;
}

我认为,采用双轨搜索法是一种非常常见的做法;我故意使用与休养一样的变称,以便更容易看到共同点。 因此,我们可以说,计算中间点再次是分裂的一部分,而 lo体的其余部分是 con。

但是,当然,如果你的审查者认为不同,他们可能难以说服他们打D&C。

Update: just had a thought that if I were to develop a generic skeleton implementation of a D&C algorithm, I would certainly use binary search as one of API suitability tests to check whether the API is sufficiently powerful while also concise. Of course it does not prove anything :)

<代码>Merge Sort和> Quick Sort 算法使用divide and conquer技术(由于存在2个子问题)和Binary search下出现减少和 conquer(因此有1个子问题)。

因此,本金搜索实际上使用了减少和混合技术,而不是鸿沟和混合技术。

资料来源:。 https://www.geeksforgeeks.org/减少-and-conquer/

进行双轨搜索是为了描述分裂和趋同,因为征服步骤并不明确。 计算法的结果是沙斯特的针索引和纯粹的D&C执行将退回最小海斯特的针头指数(单项清单中的<>0),然后在分界线的较大沙滩中重新添加冲抵。

Pseudocode to interpret:

function binary_search has arguments needle and haystack and returns index
    if haystack has size 1
       return 0
    else 
        divide haystack into upper and lower half
        if needle is smaller than smallest element of upper half
            return 0 + binary_search needle, lower half
        else
            return size of lower half + binary_search needle, upper half

The addition (0 + or size of lower half) is the conquer part. Most people skip it by providing indices into a larger list as arguments, and thus it is often not readily available.

当然,这一鸿沟将这块土地分成一半。

组成部分是确定在所处理部分是否有搜查要素,以及该部分的位置。

Dichotomic in computer science refers to choosing between two antithetical choices, between two distinct alternatives. A dichotomy is any splitting of a whole into exactly two non-overlapping parts, meaning it is a procedure in which a whole is divided into two parts. It is a partition of a whole (or a set) into two parts (subsets) that are: 1. Jointly Exhaustive: everything must belong to one part or the other, and 2. Mutually Exclusive: nothing can belong simultaneously to both parts.

多样化和 con化工作,重新将问题分解成两个或两个以上相同的次要问题,直到这些问题变得简单,可以直接解决。

因此,双轨搜索将每个回收点检查的物项数量减半,并确定如果能够确定关键点不在,该半部是否有机会找到“关键”项目,或将它移至另一半。 由于算法是分辨率,因此双轨搜索将认为,“钥匙”必须部分地存在,直到它返回了钥匙缺失的撤离状态。

多样化和公平算法基于以下三个步骤:

  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.

<>proper gap and conqueretic will requires both items to beprocess.

因此,许多人将而不是称作双轨研究,分辨率和qu算法,它确实是divide 问题,但忽略了另一个方面。

但最有可能的是,你的审查者只是想看到你是如何争辩的。 (Good) 考试涉及事实,但涉及在质疑超越原始材料时如何反应。

因此,IMHO的适当答案是:

从技术上讲,这只是一个分级步骤,但只需要把原先任务的一半相加,而另一半已经做了三边工作。

BTW:QuickSort, “QuickSelect”有冰色差异,实际上利用这种差异获得平均O(n)中值搜索算法。 它像QuickSort一样,但它只关注其中一半。

纯粹的搜索并不是一种分裂和qu的做法。 这是一种减少和趋同的做法。

In divide and conquer approach, each subproblem must contribute to the solution but in binary search, all subdivision does not contribute to the solution. we divide into two parts and discard one part because we know that the solution does not exist in this part and look for the solution only in one part.

The informal definition is more or less: Divide the problem into small problems. Then solve them and put them together (conquer). Solving is in fact deciding where to go next (left, right, element found).

http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm” rel=“nofollow”>wikipedia :

The name "divide and conquer" is sometimes applied also to algorithms that reduce each problem to only one subproblem, such as the binary search algorithm for finding a record in a sorted list.

This states, it s NOT [update: misread this phrase:)] only one part of divide and conquer.

Update: This article made it clear for me. I was confused since the definition says you have to solve every sub problem. But you solved the sub problem if you know you don t have to keep on searching..

3. 普通搜索器是一种分立和qu算法:

(1) 在多样化和公平算法中,我们试图通过解决一个小小小小小小小小的问题(自杀部分)来解决问题,并利用解决办法解决我们更大的问题。

2) 这里,我们的问题是,在分类阵列中找到一个要素。 我们能够解决这个问题,解决一个类似的次问题。 (我们在此提出次要问题,其依据是,所搜寻的要素比中间要素小或更大。) 因此,一旦我们知道这一因素肯定不会在一半时间内存在,我们就会在另一半阶段解决类似的问题。

3) 这样,我们就能够回头。

4) The conquer part here is just returning the value returned by the sub problem to the top the recursive tree

I think it is Decrease and Conquer.

这里的引自维基。

"The name decrease and conquer has been proposed instead for the single-subproblem class"

根据我的理解,“Conquer” part is at end when 你找到了宾特搜索的目标内容。 “帐篷”部分正在缩小搜索空间。

4. 普通搜索器和Ternary搜索器以减标和公平技术为基础。 由于你没有把问题分开,你实际上减少了问题,把2(3)人隔开。

Merge Sort和Quick Sort Algorithms可以作为多样化和公平技术的例子。 您将问题分成两个小问题,并利用算法重新分类。 但是,你放弃了双轨搜索中的一半阵列。 这意味着你们的阵列规模,而不是分裂。

无,双轨搜索不会分裂和qu。 是的,双轨搜索量减少,而且会减少。 我认为,分立和qu算法具有O(nlog(n))的效率,而减少和qu算法则具有O(log(n)的效率。 不同之处在于,您是否需要评价数据分部分的两部分。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签