Home > Web Front-end > JS Tutorial > body text

Example of using bubble sort for arrays in javascript

PHPz
Release: 2018-09-30 18:22:31
Original
1396 people have browsed it

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>
Copy after login

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!