Home >Web Front-end >JS Tutorial >Detailed explanation of bubble sorting in sorting in JS
This article mainly shares with you the detailed explanation of bubble sorting in sorting in JS. This article is mainly in the form of code and hopes to help everyone.
window.onload=function() { var arr=[1,14,4,2,6,10]; arr.sort(function(obj1,obj2){ if(obj1>obj2) {return 1;} else if (obj1==obj2) {return 0;} else {return -1;} }); console.log(arr); // var f1=function(a,b){return a-b;} arr.sort(f1); alert(arr); // //鍐掓场鎺掑簭 function mysort(arr) { var temp; for(var i=0;i<arr.length;i++)//姣旇緝瓒熸暟 {var isSort=true; for (var j=0;j<arr.length-i-1;j++)//姣忚稛姣旇緝娆℃暟 { if (arr[j]>arr[j+1]){ isSort=false; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp;} } if(isSort) {break;} } return arr; }
Related recommendations:
Detailed explanation of bubble sort in JavaScript
Js bubble sort and quick sort detailed explanation
Simple understanding of PHP bubble sort
The above is the detailed content of Detailed explanation of bubble sorting in sorting in JS. For more information, please follow other related articles on the PHP Chinese website!