<textarea id="runcode38621"><script> function QuickSort () {function Dosort (a,s,e) {if (s<e) { var pos=partition (a,s,e); Dosort (A,S,POS-1); Dosort (a,pos+1,e); } function partition (A,st,en) {var s=st; var e=en+1; var temp=a[s]; while (1) {while (a[++s]<temp); while (a[--e]>temp); if (s>e) break; var tem=a[s]; A[s]=a[e]; A[e]=tem; } A[st]=a[e]; A[e]=temp; return e; } dosort (this,0,this.length-1); return this; } Array.prototype.quicksort=quicksort; Alert (new Array (5,2,4,6,1). QuickSort ()); </script></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Copy Code code as follows:
How fast you're going to be Array.prototype.sort
var a=[4,723,3,5,67,32,4,43,34,545,43,43,,234,7,367,7,87,23,6,78,7957,t, 84,78,34,536,2345,4534566,67,67,54,45,797,67,8798,76543,8];
Alert (A.sort ());
That's the quickest.
Add a binary tree sort
<ptml><pead><script language = "JavaScript" > Function node (data) {this.data = da Ta var Lnode; var Rnode; This.insert = function Insert (newdata) {if (newdata<this.data) {if (this. Lnode = = null) {this. Lnode = new node (newdata); }else{this. Lnode.insert (NewData); }}else{if (this. Rnode = = null) {this. Rnode = new node (newdata); }else{this. Rnode.insert (NewData); }}} function tree () {var root; This.insertnode = function Insertnode (newdata) {if (this.root = = null) {this.ro OT = new node (newdata); This.index = 0; }else{This.root.insert (NewData); } this.inordertraversal = function inordertraversal () {//middle order Calendar This.inorder (This.root); } This.inorder = function inorder (N) {if (n!=null) {this.inorder (N.lnode); Output result document.write (N.data + " "); This.inorder (N.rnode); }}//test inserts some numbers in. var t=new tree (); T.insertnode (39); T.insertnode (69); T.insertnode (94); T.insertnode (47); T.insertnode (50); T.insertnode (72); T.insertnode (55); T.insertnode (41); T.insertnode (97); T.insertnode (73); T.inordertraversal (); </SCRIPT> </HEAD> <BODY> <table cellspacing=1 width= "75%" border=1> <tr> <TD&G T;</td></tr></table></body></ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]