Quick Sort by Javascript _ javascript skills

Source: Internet
Author: User
Sortingalgorithm is one of the oldest and most basic tasks in computer science. To become a qualified programmer, you must understand and master various sorting algorithms. Currently, there are about seven or eight common sorting algorithms. Among them, quick sorting is the most widely used and fast. It was proposed by the Turing Award winner C. A. R. Hoare (1934 --) at 1960.

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

(1) Select an element in the dataset as the "benchmark ).

(2) All elements smaller than the "benchmark" are moved to the left of the "benchmark"; all elements greater than the "benchmark" are moved to the right of the "benchmark.

(3) Repeat Step 1 and Step 2 on the left and right sides of the "benchmark" until only one element is left on all subsets.

For example, how to sort a dataset {85, 24, 63, 45, 17, 31, 96, 50?

Step 1: select the intermediate element 45 as the "benchmark ". (You can select any reference value, but it is easier to select the intermediate value .)

Step 2, compare each element with the "Reference" in order to form two subsets, one being "less than 45" and the other being "greater than or equal to 45 ".

Step 3: Repeat Step 1 and Step 2 for the two subsets until only one element is left for all subsets.

The following describes how to use Javascript to implement the above algorithm based on the information on the Internet.

First, define a quickSort function. Its parameter is an array.

var quickSort = function(arr) {};

Then, check the number of elements in the array. If the value is less than or equal to 1, return.

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

Then, select "benchmark", separate it from the original array, and define two empty arrays to store one left and one right subsets.

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, we start to traverse the array. Elements smaller than the "benchmark" are placed in the subset on the left, and elements larger than the benchmark are placed in the subset on the 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 = [];  for (var i = 0; i < arr.length; i++){    if (arr[i] < pivot) {      left.push(arr[i]);    } else {      right.push(arr[i]);    }  }};

Finally, we can get the sorted array by repeating this process recursively.

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(left).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 design javascript programs.

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.