function BubbleSort(arr){ var len = arr.length for(var i =0;i<len-1;i++){ for(var j=0;j<len-1-i;j++){ if(arr[j]>arr[j+1]){ var temp = arr[j] arr[j]=arr[j+1] arr[j+1]=temp } } } return arr } var arr = [1,3,5,7,2,4,6,8] console.log(BubbleSort(arr)) //[1,2,3,4,5,6,7,8]