JavaScript performs quick sorting and javascript performs sorting.

Source: Internet
Author: User

JavaScript performs quick sorting and javascript performs sorting.

This example describes how to implement quick sorting in JavaScript. We will share this with you for your reference. The details are as follows:

Thoughts:

Data is divided into different subsequences that contain smaller elements and larger elements by means of grouping and recursion.

1. Select an element as the benchmark in the array.

2. traverse the array. All elements smaller than the benchmark are moved to the left of the benchmark, and all elements larger than the benchmark are moved to the right of the benchmark.

3. Repeat the first two steps for the left and right subsets of the benchmark until only one element is left for all subsets.

Implementation Code:

Function sqort (arr) {if (arr. length = 0) {return [];} var left = []; var right = []; var comment = arr [0]; // (reference with the first element) for (var I = 1; I <arr. length; I ++) {if (arr [I] <distinct) {left. push (arr [I]);} else {right. push (arr [I]);} return sqort (left ). concat (histogram, qsort (right); // recursion} var a = []; for (I = 0; I <10; ++ I) {a [I] = Math. floor (Math. random () * 100 + 1);} console. log (a); console. log (sqort (a); // (when the base is an intermediate element) function sqort (arr) {if (arr. length <= 1) {return arr;} var left = []; var right = []; var comment tindex = Math. floor (arr. length/2); var partition = arr. splice (tindex, 1) [0]; // (base with intermediate element) for (var I = 1; I <arr. length; I ++) {if (arr [I] <distinct) {left. push (arr [I]);} else {right. push (arr [I]);} return sqort (left ). concat (iterator, sqort (right); // recursion} var a = [,]; console. log (a); console. log (sqort ());

Note:Recursively call small and large arrayssqort()Function. When the recursion ends, a small array is connected with the benchmark and a large array to form the final ordered array and return it.

PS: here we recommend a demo tool for sorting for your reference:

Online animation demo insert/select/bubble/merge/hill/Quick Sort Algorithm process tool:
Http://tools.jb51.net/aideddesign/paixu_ys

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.