首页 > 后端开发 > php教程 > 自学 PHP,冒泡排序和二元查找怎么也不明白,该放弃吗?

自学 PHP,冒泡排序和二元查找怎么也不明白,该放弃吗?

WBOY
发布: 2016-06-17 08:32:47
原创
1105 人浏览过

自学php,没有参加任何机构,自己看书学习。
css div阶段都很通顺的学会了,正式学php 前两周也很通顺,语法,函数,很快就掌握了,但是到了数组就开始卡壳,数组以后几乎都听不懂了。
泡沫排序我敲了20次程序,还是不知道那两个for循环是怎么想出来的。顺序查找和二元查找也完全没有学会。
这个阶段怎么度过?
我的努力程度几乎全天都在学习php,没有课的时候心无旁骛的敲当天学的代码,但就是敲不明白。
我这个情况能顺利进入项目阶段吗?该放弃吗?

回复内容:

自然而然就会了,不用刻意理解 熟能生巧 不能从看或者写代码来理解算法,要理解算法然后再理解代码

比如冒泡排序数组 $a,有 n 个元素
首先你要能够理解一个冒泡排序,从头到尾,每一步都做了什么工作
比如你能够用文字表达出来
第一步:将 $a[0] 和 $a[1] 进行比较,如果 $a[0] > $[1] 则两个值互换
第二步:将 $a[1] 和 $a[2] 进行比较,如果 $a[1] > $[2] 则两个值互换
...
直到 $[n - 1] 和 $[n] 比较完毕
这时候已经计算出最大值,并把它放在了 $a[n] 的位置上,但已经完成了一个循环,而 0 到 n - 1 之间的元素还是乱序的
那接下来就是要从 0 到 n -1 之间算出最大值,并把它移动到 n - 1 的位置上,就是要重复最上面的每一步

也就是说一个(内层)循环是为了遍历元素算出最大值,另一个(外层)循环是为了将整个步骤再计算一次

最后,编程其实是一种难度跨度比较大的工作,也就是有的工作可能需要技术很强,但是有的工作也有可能难度低到没底线,所以如果不是没兴趣了,或者先天智商缺陷,坚持就好了。 这属于数据结构的部分,可以找些数据结构的书来看。

可以看看维基百科上的“冒泡排序”条目: zh.wikipedia.org/wiki/% ,看一下C语言的那个程序,想一想两个for循环的4个数值是怎么得来的,为什么是i = 0; i
维基百科页面下方还有很多其他排序算法的链接,可以一起看一看。

jsdo.it/norahiko/oxIy/f 这里有用JavaScript写的各种排序的演示,把数量调到5,把速度调到1,慢慢地看一下整个排序过程是怎么进行的。 demo.sixpoint.me/sortin 这里是汉化版。
css div阶段都很通顺的学会了,正式学php 前两周也很通顺,语法,函数,很快就掌握了,但是到了数组就开始卡壳,数组以后几乎都听不懂了。
自学很赞成,现在的各种机构教出来的是什么样子大家都清楚。不过,什么叫“css div阶段”?HTML 4总共有多少标签?CSS 2.1总共有多少条规则?HTML是为了什么而设计出来的?CSS是为了什么而设计出来的?PHP语法本身很简单,和C基本一样。到数组就卡壳了,后面的一些结构怎么办?面向对象怎么办?
以后做项目,可能有几百个对象和类,如果连个冒泡排序都理解不了,恐怕很难理清几百个对象之间的复杂关系。找一些图灵的书,虽然难一点,但是学出来后基础会比较扎实。 我当初学C的时候看谭浩强书上的那个冒泡排序,看了几十遍都没看明白实现的思路-,也是像lz这样怀疑的自己,想着要不要学下去,后面我都不知道从哪一天开始,突然就理解了..- -!
我的建议是,看不懂的就暂时跳过,继续往后面学~可能看后面某个部分就自然明白了~ 个人愚见,刚开始的时候,我跟lz一样的,怎么都想不通.后来做的项目多了,接触的模块多了.再回头看算法,你就会如同猛然大悟一样的.原来你不知不觉已经运用了冒泡,递归,快速排序====之类的算法.
但是,基础的数据结构概念你还是要多了解些的.
加油! 冒泡排序是一种效率很低的排序算法,所谓的“泡”按我个人的理解就是数列中较大或较小的数,这个较大或较小的数不断的移动就是“冒”。

各种排序算法,先弄清楚其原理,之后将其形象化(像放动画片一样),这样学起来就比较轻松了。这一切的前提是,你对算法有兴趣。

下面是 Java 的冒泡算法:

<code class="language-java"><span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Sorter</span> <span class="o">{</span>
	
	<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
		<span class="kt">int</span><span class="o">[]</span> <span class="n">arr</span> <span class="o">=</span> <span class="n">getIntArray</span><span class="o">(</span><span class="mi">10</span><span class="o">,</span> <span class="o">-</span><span class="mi">50</span><span class="o">,</span> <span class="mi">50</span><span class="o">);</span>
		
		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">print</span><span class="o">(</span><span class="s">"Before: "</span><span class="o">);</span>
		<span class="n">printArray</span><span class="o">(</span><span class="n">arr</span><span class="o">);</span>
		<span class="n">bubbleSort</span><span class="o">(</span><span class="n">arr</span><span class="o">);</span>
		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">print</span><span class="o">(</span><span class="s">"After : "</span><span class="o">);</span>
		<span class="n">printArray</span><span class="o">(</span><span class="n">arr</span><span class="o">);</span>
	<span class="o">}</span>
	
	<span class="kd">private</span> <span class="kd">static</span> <span class="kt">int</span><span class="o">[]</span> <span class="nf">getIntArray</span><span class="o">(</span><span class="kt">int</span> <span class="n">capacity</span><span class="o">,</span> <span class="kt">int</span> <span class="n">min</span><span class="o">,</span> <span class="kt">int</span> <span class="n">max</span><span class="o">)</span> <span class="o">{</span>
		<span class="n">Random</span> <span class="n">random</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Random</span><span class="o">();</span>
		<span class="kt">int</span><span class="o">[]</span> <span class="n">arr</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[</span><span class="n">capacity</span><span class="o">];</span>
		<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"> <span class="n">capacity</span><span class="o">;</span> <span class="o">++</span><span class="n">i</span><span class="o">)</span> <span class="o">{</span>
			<span class="n">arr</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="na">nextInt</span><span class="o">(</span><span class="n">max</span> <span class="o">-</span> <span class="n">min</span><span class="o">)</span> <span class="o">+</span> <span class="n">min</span><span class="o">;</span>
		<span class="o">}</span>
		<span class="k">return</span> <span class="n">arr</span><span class="o">;</span>
	<span class="o">}</span>
	
	<span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">printArray</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">arr</span><span class="o">)</span> <span class="o">{</span>
		<span class="n">StringBuilder</span> <span class="n">sb</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="o">();</span>
		<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"> <span class="n">arr</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="o">++</span><span class="n">i</span><span class="o">)</span> <span class="o">{</span>
			<span class="n">sb</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">arr</span><span class="o">[</span><span class="n">i</span><span class="o">]).</span><span class="na">append</span><span class="o">(</span><span class="s">" "</span><span class="o">);</span>
		<span class="o">}</span>
		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">sb</span><span class="o">.</span><span class="na">toString</span><span class="o">());</span>
	<span class="o">}</span>
	
	<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">bubbleSort</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">arr</span><span class="o">)</span> <span class="o">{</span>
		<span class="k">if</span> <span class="o">(</span><span class="n">arr</span><span class="o">.</span><span class="na">length</span> <span class="o"> <span class="mi">2</span><span class="o">)</span> <span class="k">return</span><span class="o">;</span>
		<span class="kt">boolean</span> <span class="n">isSorted</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
		<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">arr</span><span class="o">.</span><span class="na">length</span> <span class="o">-</span> <span class="mi">1</span><span class="o">;</span> <span class="n">i</span> <span class="o">></span> <span class="mi">0</span> <span class="o">&&</span> <span class="o">!</span><span class="n">isSorted</span><span class="o">;</span> <span class="o">--</span><span class="n">i</span><span class="o">)</span> <span class="o">{</span>
			<span class="n">isSorted</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
			<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">j</span> <span class="o"> <span class="n">i</span><span class="o">;</span> <span class="o">++</span><span class="n">j</span><span class="o">)</span> <span class="o">{</span>
				<span class="k">if</span> <span class="o">(</span><span class="n">arr</span><span class="o">[</span><span class="n">j</span><span class="o">]</span> <span class="o">></span> <span class="n">arr</span><span class="o">[</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="o">])</span> <span class="o">{</span>
					<span class="n">swap</span><span class="o">(</span><span class="n">arr</span><span class="o">,</span> <span class="n">j</span><span class="o">,</span> <span class="n">j</span><span class="o">+</span><span class="mi">1</span><span class="o">);</span>
					<span class="n">isSorted</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
				<span class="o">}</span>
			<span class="o">}</span>
		<span class="o">}</span>
	<span class="o">}</span>
	
	<span class="kd">private</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">swap</span><span class="o">(</span><span class="kt">int</span><span class="o">[]</span> <span class="n">arr</span><span class="o">,</span> <span class="kt">int</span> <span class="n">indexA</span><span class="o">,</span> <span class="kt">int</span> <span class="n">indexB</span><span class="o">)</span> <span class="o">{</span>
		<span class="kt">int</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">arr</span><span class="o">[</span><span class="n">indexA</span><span class="o">];</span>
		<span class="n">arr</span><span class="o">[</span><span class="n">indexA</span><span class="o">]</span> <span class="o">=</span> <span class="n">arr</span><span class="o">[</span><span class="n">indexB</span><span class="o">];</span>
		<span class="n">arr</span><span class="o">[</span><span class="n">indexB</span><span class="o">]</span> <span class="o">=</span> <span class="n">temp</span><span class="o">;</span>
	<span class="o">}</span>
<span class="o">}</span>
</span></span></span></span></code>
登录后复制
还是先学习c 吧 PHP多用于网站开发,你现在碰到的是算法问题,可以先绕道而行,不影响你对PHP的学习和使用 可以过一段时间再重复学习,跟练习哑铃差不多。
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板