How to implement various sorting methods using js

亚连
Release: 2018-06-13 17:48:58
Original
1367 people have browsed it

Below I will share with you an article (detailed explanation) on the differences between various sorting methods and sort methods based on js. It has a good reference value and I hope it will be helpful to everyone.

I had a whim today and wanted to know whether the sort method has advantages over various sortings, so I made a test by referring to other people's codes. The results were surprising. Here is the code.

     图片列表生成交互组件  

Copy after login

The above method passes the test time, and then analyzes which sorting method saves time. Time is life. Using the correct method can save a lot of time, especially when big data is running.

First look at the time it takes to process an array of 10,000 lengths:

* sort sorting systemSort 11
* Bubble sort bubbleSort 169
* quick sort quickSort 144
* insertion sort insertSort 139
* Hill sort shellSort 3

Test 100,000-long array data:

* sort sorting systemSort 63
* Bubble sorting bubbleSort 16268
* Quick sorting quickSort reports an error directly
* Insertion sorting insertSort 13026
* Hill sorting shellSort 8

Test an array with a length of one million:

* sort sort systemSort 575
* Bubble sort bubbleSort time unknown
* Quick sort quickSort reports an error directly
* Insertion sort insertSort crashes directly
* Hill sort shellSort 93

Test an array of 10 million long:

* sort sorting systemSort 7039
* Bubble sort bubbleSort not tested
* quick sort quickSort not tested
* insertion sort insertSort not tested
* Hill sorting shellSort 1225

Testing an array of 100 million long:

* sort systemSort crashes directly
* Bubble sort bubbleSort not tested
* Fast Sort quickSort Not tested
* Insertion sort insertSort Not tested
* Hill sort shellSort 19864

Finally passed the test,In the worst case, I found that Hill sorting is still the best, and it is even faster than the system sorting. It is really surprising. In this way, everyone can see what method needs to be used for sorting under what circumstances.

Then We test under random conditions:

     图片列表生成交互组件  

Copy after login

Test a 10 million-long array:

* sort sorting systemSort 8842
* Bubble sort bubbleSort not tested
* Quick sort quickSort not tested
* Insertion sort insertSort 45
* Hill sort shellSort 1133

In unknown situations Under better circumstances, insertion sort is the most efficient

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the separation and combination of vue-admin and backend (flask)

Detailed explanation of the basics in Vue webpack Configuration

Explain the basic configuration in detail in Vue webpack

How to build a multi-person chat room in the nodejs express environment

The above is the detailed content of How to implement various sorting methods using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!