Home  >  Article  >  Web Front-end  >  JavaScript implements quick sort analysis

JavaScript implements quick sort analysis

小云云
小云云Original
2018-01-11 09:06:271634browse

This article mainly introduces the method of implementing quick sort in JavaScript. It analyzes the principle, implementation method and related operation precautions of quick sort in the form of examples. Friends in need can refer to it. I hope it can help everyone.

Idea:

Use the divide-and-conquer idea and recursive method to decompose the data into different subsequences containing smaller elements and larger elements.

1. Select an element in the array as the base

2. Traverse the array. Elements smaller than the base are moved to the left of the base, and elements larger than the base are moved to the right of the base.

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

Implementation code:


function sqort(arr){
 if(arr.length===0){
 return [];
}
var left=[];
var right=[];
var pivot=arr[0];//(基准以首元素)
for(var i=1;i

Note: Recursively call the sqort() function for smaller arrays and larger arrays respectively , when the recursion ends, the smaller array is connected to the base and the larger array to form the final ordered array and returned.

Related recommendations:

Example of PHP implementation of quick sorting method

Example of PHP implementation of quick sorting algorithm for two-dimensional arrays

How to implement quick sort

The above is the detailed content of JavaScript implements quick sort analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn