JavaScript implementation of fast sorting (Quicksort) algorithm detailed _javascript skills

Source: Internet
Author: User

At present, the most common sort algorithm is about seven or eight kinds, among which "quick sort" (Quicksort) is used most widely and faster. It is the Turing Prize winner C. A. R. Hoare (1934--) was presented at 1960.

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, using JavaScript language to achieve 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);
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 to concat (pivot, QuickSort (right));
 
var dataarray = [85,24,63,45,17,31,96,50];
Console.log (Dataarray.join (","));
Console.log (QuickSort (DataArray). Join (","));

I hope this article will help you with your JavaScript programming.

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.