Home > Backend Development > PHP Tutorial > In-depth understanding of PHP bubble sorting algorithm_PHP tutorial

In-depth understanding of PHP bubble sorting algorithm_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:07:41
Original
869 people have browsed it

The basic idea of ​​exchange sorting: compare the data to be sorted pairwise. If a reverse order occurs, swap them until all the data is sorted.
•Basic idea of ​​bubble sorting:
1. Scan all the data from back to front. If two adjacent numbers are in reverse order, they will Change. --The first bubbling
2. From back to front, scan the last to second data. If two adjacent numbers are in reverse order, swap them. --The second bubbling
3. Proceed in this order until n-1 bubblings are performed, or in a certain bubbling, it can end early if there is no reverse order.

Copy code The code is as follows:

<script><br>var arr = [15,8,7,9 ,10,0]; <p>var _len = arr.length;</p> <p>alert("Before sorting:"+arr);</p> <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> <P>alert("After sorting: "+ arr);</P> <P></script>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327530.htmlTechArticleThe basic idea of ​​exchange sorting: compare the data to be sorted pairwise, and if a reverse order occurs, exchange them until all Until the data is sorted. The basic idea of ​​bubble sort: 1. From back to front...
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