// Sorting Algorithms int JavaScript /** * file Sort.js * 1. Bubble Sort冒泡排序法 */ function BubbleSort(arry, nszie) { var i, j, temp; var swapped; for (i = 0; i < nszie - 1; i++) { swapped = false; for (j = 0; j < nszie - i - 1; j++) { if (arry[j] > arry[j + 1]) { // Swap arry[j] and arry[j+1] temp = arry[j]; arry[j] = arry[j + 1]; arry[j + 1] = temp; swapped = true; } } // IF no two elements were // swapped by inner loop, then break if (swapped == false) break; } return arry } function stringArray(arry, size) { //console.log(arry); var myStr=new Array(); var getstr=""; /**/ var i; for (i = 0; i < size; i++) { getstr=getstr+arry[i].toString() + "<br/>"; myStr[i]=arry[i].toString(); console.log(myStr[i]); } return getstr;//myStr.join(" "); } /** */ function printArray(arry, size) { var getstr=""; var i; for (i = 0; i < size; i++) { console.log(arry[i] + " "); getstr=getstr+arry[i]+" "; } return getstr; }
调用:
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>成长开始,geovindu,涂聚文,Geovin Du</title> <meta name="Description" content="geovindu"/> <meta name="Keywords" content="geovindu"/> <meta name="author" content="geovindu"/> <script src="js/jquery-3.6.0.js"></script> <script src="js/SortAlgorithm/Sort.js"></script> <script type="text/javascript"> $(document).ready(function () { // 1. Bubble Sort冒泡排序法 var arry = [ 64, 34, 25, 112, 220, 11, 90 ]; var nzie= arry.length; var geovindu=BubbleSort(arry, nzie); console.log(geovindu) var myStr=new Array(); var i; for (i = 0; i < nzie; i++) { //getstr=getstr+arry[i].toString() + "<br/>"; myStr[i]=geovindu[i].toString(); console.log(geovindu[i].toString()); } console.log(myStr.join("<br/>")); //stringArray(arry,nsize) console.log("Bubble Sorted array: "); var getstr=printArray(arry, nzie); console.log("str:"+getstr) $("#txtgeovindu").html(getstr); txtgeovindu.innerHTML = getstr;//stringArray(geovindu,nsize); $("#geovindu").html("1.泡冒泡排序Bubble Sorted:<br/>"+myStr.join("<br/>")); }); </script> </head> <body> <textarea id="txtgeovindu" class="geovindu" name="" cols="30" rows="10"></textarea> <div id="geovindu"></div> </body> </html>
标签:Sort,console,log,javascript,getstr,var,myStr,Bubble,arry From: https://www.cnblogs.com/geovindu/p/17739311.html