xiekch 2011-04-12
代码如下:
<!doctype html> <html> <head><title>javascript直接插入排序</title> <meta charset = "utf-8" /> </head> <body> <script> var arr = []; for(var i=0;i<20;++i) { arr.push(~~(Math.random()*20)); } document.write(arr+"<br/>"); Array.prototype.insertionSort = function() { var j; var value; for(var i=1;i<this.length;i++) { j=i; value = this[j]; while(j>0 && this[j-1]>value) { this[j] = this[j-1]; j--; } this[j] = value; } } arr.insertionSort(); document.write(arr+"<br/>"); </script> </body> </html>