# Data sort who is fastest (Array.prototype.sort PK quick Sort in JavaScript) _javascript tips

Source: Internet
Author: User
Tags rand

But to my surprise, there is a netizen replied that JavaScript in the array itself the sort method is the fastest, faster than the fast sorting algorithm, then saw very depressed, because then spent a good long time in the sorting algorithm, incredibly forgot the array itself sort method
But is the sort method built into JavaScript really faster than the fast sort algorithm?
Haha, the test will not know the
Let's talk about the environment I tested.
1, my test environment is IE6.0 and firefox2.0.
2, each algorithm has a number of different implementation methods, the following test I choose the above user implementation of the fast sorting algorithm, but the embedded function moved to the outside
3, the speed of the algorithm is related to the type, size and amount of data, I only compare the order of integers less than 999999, the amount of data is 500, 2000, 30000, respectively.

About the Sort method: the Sort method is a built-in method of array: This is defined in the JavaScript authoritative guide:

The sort () method sorts the elements of an array in place:no copy of the ' array is made. If sort () is called with no arguments, the elements of the array are arranged in alphabetical order (more precisely, the Order determined by the character encoding). To does this, elements are the converted to strings, if necessary, and so that they can be compared.
If you are want to sort the array of elements in some and you are must supply a comparison function that compares values and returns a number indicating their relative order. The comparison function should take two arguments, A and B, and should return one of the following:

The Sort method can accept arguments of a function type to customize its own sorting logic, and when no arguments are supplied, the default is sorted by character order, so the order of integers requires a parameter of the function type, and this test is invoked as follows:

Array.Sort (function (a,b) {return a-b})

Of course, if you want to sort the same number of integers, do not provide parameters returned by the same result, the test will know:

Copy Code code as follows:

<script>
Alert ([3,4,5,11,1].sort ())
Alert ([3,4,5,11,1].sort (function (a,b) {return a-b})
</script>

Tip: You can modify the run again first
The result is [1, 11, 3, 4, 5], obviously not the result we want
Generate the array that needs to be sorted, in order to get a fair result I first randomly generate an array for sorting, each of the two methods used in the comparison has the same elements, the following is the code that generates the random array
Copy Code code as follows:

function Random (m,n) {
To generate an integer between M and N
var i=math.random ();
Return Math.Round ((n-m) *i+m);
}

function Getrandomarr (m,n,l) {
M: generates the lowest value of the integer, N: The maximum value of the resulting integer, L: The length of the resulting array
var resultarr=[];
for (Var i=0;i<l;i++) {
Resultarr.push (Random (M,n))
}
return Resultarr;
}

The implementation of the fast sorting algorithm, this algorithm from the 51JS forum I saw this user's implementation, the code is as follows:
Copy Code code as follows:

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;
}

Array.prototype.quicksort=function () {
Dosort (this,0,this.length-1);
}

Check that the results are correct using array.join () to determine the performance test code as follows:
Copy Code code as follows:

function sortintf (a,b) {return a-b}
function pk (num) {
Num: Number of elements in the array used for sorting
To generate an array for sorting
var Arr=getrandomarr (1,999999,num);
When the number of elements is less than 10000, the N-time average is performed
var n=math.ceil (10000/num);
Generate multiple copies of the array used for sorting
var quicksortarrs=[];
var sortarrs=[];
for (Var i=0;i<n;i++) {
Quicksortarrs.push (Arr.slice (0));
Sortarrs.push (Arr.slice (0));
}
var t1=new Date ();
for (Var i=0;i<n;i++) {
Quicksortarrs[i].quicksort ();
}
var t2=new Date ();
for (Var i=0;i<n;i++) {
Sortarrs[i].sort (sortintf);
}
var t3=new Date ();
Alert ("Performance comparison, for arrays of" +num+ "elements, the average time spent per sort is as follows: \ n"
+ "Array.prototype.sort:" + ((T3-T2)/N) + "ms\n"
+ "QuickSort:" + ((T2-T1)/N) + "ms\n"
);
Alert ("Sorting result is correct:" + (Sortarrs[0].join () ==quicksortarrs[0].join ());
}

Call the PK function directly, for example, you want to 300 elements of the array to sort performance comparisons, call PK (300) on it

The complete test code is as follows:
<script> function rand (M,N) {//Generate an integer var i=math.random () between M and N; Return Math.Round ((n-m) *i+m); function Getrandomarr (m,n,l) {//m: Generates the smallest value of the integer, N: The maximum value of the resulting integer, L: The length of the generated array var resultarr=[]; for (Var i=0;i<l;i++) {Resultarr.push (rand (M,N))} return Resultarr; 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; The function Dosort (a,s,e) {if (s<e) {var pos=partition (a,s,e); Dosort (A,S,POS-1); Dosort (a,pos+1,e); } Array.prototype.quickSort = function () {dosort (this,0,this.length-1); The function sortintf (a,b) {return a-b} function pk (num) {//num: The number of elements used to sort the array//generate the array var arr=getrandomarr for sorting (1,999999 , num); When the number of elements is less than 10000, the N-time average value of Var N=math.ceil (10000/num) is performed; Generates multiple copies of the array used for sorting var quicksortarrs=[]; var sortarrs=[]; for (Var i=0;i<n;i++) {Quicksortarrs.push (Arr.slice) (0)); Sortarrs.push (Arr.slice (0)); var t1=new Date (); for (Var i=0;i<n;i++) {quicksortarrs[i].quicksort (); var t2=new Date (); for (Var i=0;i<n;i++) {sortarrs[i].sort (sortintf); var t3=new Date (); Alert ("Performance comparison, for an array of" +num+ "elements, the average time spent on each sort is as follows: \ n" + "Array.prototype.sort:" + ((T3-T2)/N) + "ms\n" + "QuickSort:" + ((T2-T1)/ N) + "ms\n"); Alert ("Sorting result is correct:" + (Sortarrs[0].join () ==quicksortarrs[0].join ()); PK (500); PK (2000); PK (30000); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Test results
First first time (MS)
500 elements: ie6.0:sort:38.3 39.05 39.05
quicksort:8.6 8.6 9.4
ff2.0:sort:3.1 3.15 3.9
quicksort:4.7 4.7 3.15

2000 elements: ie6.0:sort:200 203.2 203
quicksort:40.6 43.6 43.8
ff2.0:sort:18.8 18.6 18.8
quicksort:18.6 15.6 15.6

30,000 elements: ie6.0:sort:10360 9765 9203
quicksort:843 813 891
FF2.0:SORT:422 422 406
quicksort:328 297 407
As you can see from the results,
In ie6.0, the fast sort algorithm is much faster than the array object's sort method, and for fewer elements, the fast sort speed is roughly 5 times times the sort method, and the quick sort for 30,000 elements is more than 10 times times the speed of the sort method.
In ff2.0, the speed of the two sort algorithms is basically the same, the fast sort algorithm is a little faster, which also shows that the sort of the array object in ff2.0 is more efficient, perhaps it is a quick sort, because it is close to the data of the fast sorting algorithm.
Description: The above test only represents my test results on this machine, perhaps the results on your machine will be very different, I hope we can also help test

Related Keywords:
Related Article

### Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.