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>
http://www.bkjia.com/PHPjc/327530.htmlwww.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...