Home >Web Front-end >JS Tutorial >Detailed explanation of bubble sorting in sorting in JS

Detailed explanation of bubble sorting in sorting in JS

小云云
小云云Original
2018-03-19 17:23:521783browse

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!

Statement:
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