First, you need a function that automatically generates arrays
// Functions that automatically generate arrays function randomArr (n) { let arr = []; for (let i = 1; i <= n;
i++) { arr.push(Math.floor(Math.random() * (i + 1))); } return arr; }
* Execute the above function , It's coming arr1 Array length is 50000, because js The execution speed is very fast , Only when the length is very large , To see the difference in the execution speed of each method
* be careful arr2 reach arr6 You can't simply assign values , otherwise arr1 After the change ,arr2 reach arr6 It has changed accordingly // Six identical arrays And the array length must be large enough to be compared
let arr1 = randomArr(50000); let arr2 = [...arr1]; let arr3 = [...arr1]; let
arr4 = [...arr1]; let arr5 = [...arr1]; let arr6 = [...arr1];
The next is our usual sort
// Select sort function pickSort (arr) { let temp; for (let i = 0; i < arr.length;
i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { temp =
arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } // Bubble sort function
bubleSort (arr) { let temp, isSort; for (let i = 1; i < arr.length; i++) {
isSort = false; for (let j = 0; j < arr.length - i; j++) { if (arr[j] > arr[j +
1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; isSort = true; } }
if (!isSort) { break; } } return arr; } // Quick sort The quick sort method is also recursive function quickSort
(arr) { if (arr.length <= 1) { return arr; } let x = arr.splice(0, 1)[0]; let
left = [], right = []; for (let i = 0; i < arr.length; i++) { if (arr[i] < x) {
left.push(arr[i]); } else { right.push(arr[i]); } } return
quickSort(left).concat([x]).concat(quickSort(right)); } // Insert sort function
spliceSort (arr) { let temp; for (let i = 1; i < arr.length; i++) { for (let j
= i; j > 0; j--) { if (arr[j] < arr[j - 1]) { temp = arr[j]; arr[j] = arr[j -
1]; arr[j - 1] = temp; } else { break } } } return arr; } // Shell Sort function
shellSort (arr) { let temp; let gap = 1; while (gap < arr.length) { gap = 3 *
gap + 1; } for (; gap > 0; gap = Math.floor(gap / 3)) { for (let i = gap; i <
arr.length; i ++) { for (let j = i; j > 0; j -= gap) { if (arr[j] < arr[j -
gap]) { temp = arr[j]; arr[j] = arr[j - gap]; arr[j - gap] = temp; } else {
break } } } } return arr; }
Calculate the time taken by each method

* last , You need a function to call the above sorting methods , And calculate the time // Function to calculate sort time function calcTime (arr, fun) {
console.time(fun.name); let newArr = fun(arr); console.timeEnd(fun.name); } //
Start the calculation calcTime(arr1,bubleSort); calcTime(arr2,quickSort);
calcTime(arr3,pickSort); calcTime(arr4,spliceSort); calcTime(arr5,shellSort);
Array.protype.sort yes js Self contained sorting function , You can also test the speed
// look down array.prototype How efficient is sorting in console.time('Array.prototype.sort');
arr6.sort(function (a,b) { return a-b; });
console.timeEnd('Array.prototype.sort');
Here comes the calculation

bubleSort: 6846.92724609375ms
quickSort: 342.636962890625ms
pickSort: 5732.3818359375ms
spliceSort: 859.482177734375ms
shellSort: 58.785888671875ms
Array.prototype.sort: 69.878173828125ms

Summary

Shell Sort > Array.prototype.sort > Quick sort > Insert sort > Select sort > Bubble sort

Technology
©2019-2020 Toolsou All rights reserved,
hive compress &&hdfs Merge small files I've been drinking soft water for three years ? What is the use of soft water and water softener 《 League of Heroes 》 Mobile game open public test : support iOS/Android Dual platform The situation of receiving and receiving multi-path VaR - Value at risk - Monte Carlo method - Pythonnumpy: The creation of multidimensional array uniapp Summary of page value transfer use C Language makes a very simple airplane game Docker Container data volume ,DockerfileJavaScript Medium Call and Apply