Bubble sorting of arrays is very practical, but there are still some students who don’t know how to do it, so I will introduce it in detail in this article. Friends who are interested should not miss it.
<html> <head> <title>数组的排序</title> <script> var arr = [2,4,9,11,6,3,88]; //采用冒泡排序,向上冒泡,最小值在最上边 for(var x = 0 ; x < arr.length; x++){//控制趟数 for(var y = x + 1 ; y < arr.length ; y++){ //依次比较,如果后面的元素大于前面的元素则交换 if(arr[x] > arr[y]){ var temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } document.write(arr); </script> </head> <body> <p id="time"></p> </body> </html>
The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!