JavaScript algorithm series of fast Sorting (Quicksort) algorithm examples of detailed _javascript skills

Source: Internet
Author: User

The idea of "quick sort" is simple, and the whole sort process takes only three steps:

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

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

(3) for the two subsets to the left and right of the "datum", repeat the first and second steps until all subsets are left with only one element.

For example, now there is a dataset {85, 24, 63, 45, 17, 31, 96, 50}, how do you sort it?

The first step is to select the middle element 45 as the "benchmark". (Datum values can be selected arbitrarily, but it is easier to understand the median value.) )

In the second step, each element is compared to the "datum" in order to form two subsets, one "less than 45" and the other "greater than or equal to 45".

In the third step, repeat the first and second steps for the two subsets until all subsets are left with only one element.

The following reference to the online data (here and here), using JavaScript language to implement the above algorithm.

First, define a quicksort function, whose argument is an array.

var quickSort = function (arr) {
};

Then, check the number of elements in the array and return if it is less than or equal to 1.

var quickSort = function (arr) {
if (arr.length <= 1) {return arr;}
};

Next, select "Datum" (pivot) and separate it from the original array, then define two empty arrays to hold two subsets of the left and right.

var quickSort = function (arr) {
if (arr.length <= 1) {return arr;}
var pivotindex = Math.floor (ARR.LENGTH/2);
var pivot = Arr.splice (pivotindex, 1) [0];
var left = [];
var right = [];
};

Then, start traversing the array, less than the elements of "datum" into the left subset, and the elements larger than the datum into the right subset.

var quickSort = function (arr) {
if (arr.length <= 1) {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]);
}
}
;

Finally, using recursion to repeat the process, you can get the sorted array.

var quickSort = function (arr) {
if (arr.length <= 1) {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);
};

When used, direct call to Quicksort () on the line.

The above is a small set to introduce the JavaScript algorithm series of fast Sorting (Quicksort) algorithm examples, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.