<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>中文Flex例子 &#187; 递归</title>
	<atom:link href="http://blog.minidx.com/tag/%e9%80%92%e5%bd%92/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.minidx.com</link>
	<description>中文Adobe Flex例子,Flex实例教程,RIA资源,全文检索技术,算法和数据结构</description>
	<lastBuildDate>Thu, 31 Mar 2011 03:22:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>二分搜索算法(折半查找)原理以及递归（recuition），迭代（iteration）的两种实现源代码</title>
		<link>http://blog.minidx.com/2008/02/03/468.html</link>
		<comments>http://blog.minidx.com/2008/02/03/468.html#comments</comments>
		<pubDate>Sun, 03 Feb 2008 11:59:05 +0000</pubDate>
		<dc:creator>Minidxer</dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[recuition]]></category>
		<category><![CDATA[二分搜索]]></category>
		<category><![CDATA[折半查找]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[迭代]]></category>
		<category><![CDATA[递归]]></category>

		<guid isPermaLink="false">http://blog.minidx.com/2008/02/03/468.html</guid>
		<description><![CDATA[折半查找法也称为二分查找法，它充分利用了元素间的次序关系，采用分治策略，可在最坏的情况下用O(log n)完成搜索任务。
【基本思想】
将n个元素分成个数大致相同的两半，取a[n/2]与欲查找的x作比较，如果x=a[n/2]则找到x，算法终止。如果x&#60;a[n/2]，则我们只要在数组a的左半部继续搜索x（这里假设数组元素呈升序排列）。如果x&#62;a[n/2]，则我们只要在数组a的右半部继续搜索x。
二分搜索法的应用极其广泛，而且它的思想易于理解。第一个二分搜索算法早在1946 年就出现了，但是第一个完全正确的二分搜索算法直到1962年才出现。Bentley在他的著作《Writing Correct Programs》中写道，90%的计算机专家不能在2小时内写出完全正确的二分搜索算法。问题的关键在于准确地制定各次查找范围的边界以及终止条件的确定，正确地归纳奇偶数的各种情况，其实整理后可以发现它的具体算法是很直观的。








C++描述
Download: BinarySearch.cpptemplate&#60;class Type&#62; 
int BinarySearch(Type a[],const Type&#38; x,int n)&#160;
{&#160;
int&#160;left=0; 
int right=n-1; 
while(left&#60;=right){&#160;
int&#160;middle=(left+right)/2; 
if&#160;(x==a[middle]) return middle; 
if&#160;(x&#62;a[middle]) left=middle+1; 
else&#160;right=middle-1; 
}&#160;
return -1; 
}
递归实现（recuition）
Download: binary_search_recuition.cpptemplate&#60;class Record, class Key&#62;
int binary_search( Record * r, const int &#38; low, const int &#38; high, const Key &#38; k )
{
int&#160;mid = (low + high)/2;
if(&#160;low &#60; high )
{
&#160; if(&#160;k &#60;= [...]]]></description>
		<wfw:commentRss>http://blog.minidx.com/2008/02/03/468.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>汉诺塔算法的递归与非递归的C以及C++源代码</title>
		<link>http://blog.minidx.com/2008/01/30/457.html</link>
		<comments>http://blog.minidx.com/2008/01/30/457.html#comments</comments>
		<pubDate>Tue, 29 Jan 2008 23:50:05 +0000</pubDate>
		<dc:creator>Minidxer</dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[勃拉玛]]></category>
		<category><![CDATA[汉诺塔]]></category>
		<category><![CDATA[河内塔]]></category>
		<category><![CDATA[源代码]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[递归]]></category>

		<guid isPermaLink="false">http://blog.minidx.com/2008/01/30/457.html</guid>
		<description><![CDATA[ 汉诺塔（又称河内塔）问题其实是印度的一个古老的传说。
开天辟地的神勃拉玛（和中国的盘古差不多的神吧）在一个庙里留下了三根金刚石的棒，第一根上面套着64个圆的金片，最大的一个在底下，其余一个比一个小，依次叠上去，庙里的众僧不倦地把它们一个个地从这根棒搬到另一根棒上，规定可利用中间的一根棒作为帮助，但每次只能搬一个，而且大的不能放在小的上面。计算结果非常恐怖(移动圆片的次数)18446744073709551615，众僧们即便是耗尽毕生精力也不可能完成金片的移动了。








 
算法介绍：其实算法非常简单，当盘子的个数为n时，移动的次数应等于2^n &#8211; 1（有兴趣的可以自己证明试试看）。后来一位美国学者发现一种出人意料的简单方法，只要轮流进行两步操作就可以了。首先把三根柱子按顺序排成品字型，把所有的圆盘按从大到小的顺序放在柱子A上，根据圆盘的数量确定柱子的排放顺序：若n为偶数，按顺时针方向依次摆放 A B C；若n为奇数，按顺时针方向依次摆放 A C B。（1）按顺时针方向把圆盘1从现在的柱子移动到下一根柱子，即当n为偶数时，若圆盘1在柱子A，则把它移动到B；若圆盘1在柱子B，则把它移动到C；若圆盘1在柱子C，则把它移动到A。（2）接着，把另外两根柱子上可以移动的圆盘移动到新的柱子上。即把非空柱子上的圆盘移动到空柱子上，当两根柱子都非空时，移动较小的圆盘。这一步没有明确规定移动哪个圆盘，你可能以为会有多种可能性，其实不然，可实施的行动是唯一的。（3）反复进行（1）（2）操作，最后就能按规定完成汉诺塔的移动。
所以结果非常简单，就是按照移动规则向一个方向移动金片：如3阶汉诺塔的移动：A→C,A→B,C→B,A→C,B→A,B→C,A→C
汉诺塔问题也是程序设计中的经典递归问题，下面我们将给出递归和非递归的不同实现源代码。
●汉诺塔算法的递归实现C++源代码
#include &#60;fstream&#62;
#include &#60;iostream&#62;
using namespace std;
ofstream fout("out.txt");
void Move(int n,char x,char y)
{
  fout&#60;&#60;"把"&#60;&#60;n&#60;&#60;"号从"&#60;&#60;x&#60;&#60;"挪动到"&#60;&#60;y&#60;&#60;endl;
}
void Hannoi(int n,char a,char b,char c)
{
      if(n==1)
              Move(1,a,c);
    else
    {
 [...]]]></description>
		<wfw:commentRss>http://blog.minidx.com/2008/01/30/457.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 2/16 queries in 0.042 seconds using disk
Object Caching 232/467 objects using disk

Served from: blog.minidx.com @ 2012-02-10 00:58:58 -->
