基于php冒泡排序算法的深入理解_php技巧

WBOY
Release: 2016-05-17 09:02:03
Original
1605 people have browsed it

交换排序的基本思想:两两比较待排序的数据,如果发生逆序,则交换之,直到全部数据都排好序为止。
•冒泡排序的基本思想:
1.从后往前,扫描所有的数据,如果相邻的两个数发生逆序,则互换。--第1趟冒泡
2.从后往前,扫描最后一个到第2个数据,如果相邻的两个数发生逆序,则互换。--第2趟冒泡
3.如此依次进行,直到进行n-1趟冒泡,或者在某趟冒泡中,没有逆序的情况即可提前结束。

复制代码 代码如下:

<script><BR>var arr = [15,8,7,9,10,0]; <P>var _len = arr.length; <P>alert("排序之前:"+arr); <P>var exchange=0;<BR>var temp = 0;<BR>for(var i=0; i<arr.length;i++)<BR>{<BR> exchange=0;<BR> for(var j=arr.length;j>=i;j--)<BR> {<BR> if(arr[j] < arr[i])<BR> {<BR> temp = arr[j];<BR> arr[j] = arr[i];<BR> arr[i] = temp;<BR> exchange = 1; <BR> }<BR> }<BR> if(exchange == 0)<BR> {<BR> break;<BR> }<BR>} <P>alert("排序之后:"+ arr); <P></script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template