Bubble Sort
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bubble Sort相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <script type="text/javascript"> Array.prototype.bubble_sort = function(){ var i, j, temp; for (i = 0; i < this.length - 1; i++) { for (j = 0; j < this.length - 1 - i; j++) { if(this[j] > this[j + 1]){ temp = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } }; }; return this; } Array.prototype.bubble_sort2 = function(){ var i, j, temp; for (i = 0; i < this.length - 1; i++) { for (j = 0; j < this.length - 1 - i; j++) { if(this[j] < this[j + 1]){ temp = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } }; }; return this; } var num = [22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70]; num.bubble_sort(); for (var i = 0; i < num.length; i++) document.body.innerHTML += num[i] + " "; document.body.innerHTML += "<br>"; num.bubble_sort2(); for (var i = 0; i < num.length; i++) document.body.innerHTML += num[i] + " "; </script> </body> </html>
以上是关于Bubble Sort的主要内容,如果未能解决你的问题,请参考以下文章