Home >Web Front-end >JS Tutorial >JavaScript quick sort_javascript tips

JavaScript quick sort_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:21:251011browse

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]
The code is as follows:


No matter how fast you are, you can’t be faster than 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());
This is the fastest

Add one Binary tree sorting

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute <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>]
Statement:
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