JavaScript Quick Sort Method

Source: Internet
Author: User

The idea of "quick sorting" is simple, and the entire sequencing process takes only three steps:

(1) In the dataset, select an element as the "datum" (pivot).

(2) All elements smaller than "datum" are moved to the left of "datum", and all elements that are greater than "datum" are moved to the right of "datum".

(3) for the two subsets to the left and right of the Datum, repeat the first and second steps until there is only one element left in all the subsets.

var function quickSort (arr) {

if (arr.length<=0) {

return arr;

}

var pivotindex = Math.floor (ARR.LENGTH/2);

var pivot = Arr.splice (pivotindex, 1) [0];

var left = [];

var right = [];

for (var i = 0; i < arr.length; i++) {

if (Arr[i] < pivot) {

Left.push (Arr[i]);

} else {

Right.push (Arr[i]);

}

}

Return QuickSort (left). Concat ([pivot], QuickSort (right));//use recursion to repeat the process, you can get the sorted array.

}

 

JavaScript Quick Sort Method

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.