Who is the fastest sort of data (Array. prototype. sortPK in javascript) _ javascript skills

Source: Internet
Author: User
Today, in the 51js forum, a netizen published a fast sorting algorithm implemented by ctipt PT, which also involved the data sorting application in javasctipt in the previous days. In order to increase the sorting speed, it uses a fast sorting algorithm. But What surprised me was that, one of the following netizens replied that the sort method of Array itself in javascript was the fastest, faster than the fast sorting algorithm. At that time, I was very depressed, because it took a long time on the Sorting Algorithm, I forgot the sort method of Array.
But is the built-in sort method in javascript faster than the quick sorting algorithm?
Haha, I don't know after testing.
Let's talk about the environment I tested.
1. My testing environments are IE6.0 and firefox2.0.
2. There are many different implementation methods for each algorithm. In the following test, I chose the fast sorting algorithm implemented by the above netizens, But I just moved the embedded function out.
3. The algorithm execution speed is related to the data type, size, and data volume. Here I Only sort the data by an integer smaller than 999999. The data volume is set to 500, 2000, and 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 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 do this, elements are first converted to strings, if necessary, so that they can be compared.
If you want to sort the array elements in some other order, you must supply a comparison function that compares two values and returns a number indicating their relative order. the comparison function shocould take two arguments, a and B, and shoshould return one of the following:

The sort method can accept a function-type parameter to customize its own sorting logic. If no parameter is provided, it is sorted by character by default, therefore, you need to provide a function-type parameter for sorting integers. The call method in this test is as follows:

Array. sort (function (a, B) {return a-B })

Of course, if the numbers of Integers to be sorted are the same and the results returned by parameters are not provided, you can test the result as follows:

The Code is as follows:

Alert ([3, 4, 5, 11, 1]. sort ())
Alert ([3, 4, 5, 11, 1]. sort (function (a, B) {return a-B }))

Tip: you can modify it before running it.
The result is [1, 11, 3, 4, 5], which is obviously not the result we want.
Generates an array to be sorted. In order to obtain fair results, I randomly generate an array for sorting. The arrays used in each comparison have the same elements, the following code generates a random array:

The Code is as follows:

Function random (m, n ){
// Generate an integer between m and n
Var I = Math. random ();
Return Math. round (n-m) * I + m );

Function getRandomArr (m, n, l ){
// M: generate the minimum integer, n: generate the maximum integer, and l: the length of the generated Array
Var resultArr = [];
For (var I = 0; I ResultArr. push (random (m, n ))
Return resultArr;

The implementation of the quick sorting algorithm is based on the implementation of the netizen on the 51js forum that I saw. The Code is as follows:

The Code is as follows:

Function doSort (a, s, e)
If (s {
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] 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 whether the results are correctly determined using array. join (). The performance test code is as follows:

The Code is as follows:

Function sortIntF (a, B) {return a-B}
Function pk (num ){
// Num: number of elements in the array used for sorting
// Generate an array for sorting
Var arr = getRandomArr (1,999999, num );
// When the number of elements is less than 10000, the average value is obtained n times.
Var n = Math. ceil (10000/num );
// Generate multiple copies of the sorted Array
Var quickSortArrs = [];
Var sortArrs = [];
For (var I = 0; I QuickSortArrs. push (arr. slice (0 ));
SortArrs. push (arr. slice (0 ));
Var t1 = new Date ();
For (var I = 0; I QuickSortArrs [I]. quickSort ();
Var t2 = new Date ();
For (var I = 0; I SortArrs [I]. sort (sortIntF );
Var t3 = new Date ();
Alert ("performance comparison, for arrays of" + num + "elements, the average time consumed for each sort is as follows: \ n"
+ "Array. prototype. sort:" + (t3-t2)/n) + "ms \ n"
+ "QuickSort:" + (t2-t1)/n) + "ms \ n"
Alert ("Whether the sorting result is correct:" + (sortArrs [0]. join () = quickSortArrs [0]. join ()));

You can directly call the pk function. For example, if you want to compare the sorting performance of the array with 300 elements, you can call the pk (300) function.

The complete test code is as follows:

Script function rand (m, n) {// generates an integer var I = Math between m and n. random (); return Math. round (n-m) * I + m);} function getRandomArr (m, n, l) {// m: generate the minimum integer, n: generate the maximum 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;} 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);} function sortIntF (a, B) {return a-B} function pk (num) {// num: number of elements in the array for sorting // generate the array var arr = getRandomArr (10000, num) for sorting; // when the number of elements is less, run n times to obtain the average value 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 in each sort is as follows: \ n "+" Array. prototype. sort: "+ (t3-t2)/n) +" ms \ n "+" quickSort: "+ (t2-t1)/n) +" ms \ n "); alert ("Whether the sorting result is correct:" + (sortArrs [0]. join () = quickSortArrs [0]. join ();} pk (500); pk (2000); pk (30000); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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

30000 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 sorting algorithm is much faster than the sort method of the Array object. for fewer elements, the fast sorting speed is about 5 times faster than that of the sort method, quick sorting of 30000 elements is dozens of times faster than the sort method.
In ff2.0, the speed of the two sort algorithms is almost the same, while that of the Quick sort Algorithm is slightly faster. This also shows that the sort of Array objects in ff2.0 is relatively efficient, maybe it is a quick sort, because it is very close to the data of the Quick Sort Algorithm
Note: The above tests only represent the test results on my local machine. The results may differ greatly from those on your machine. I hope you can also test them.
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.