Home > Web Front-end > JS Tutorial > body text

How to implement quick sort using JavaScript (detailed tutorial)

亚连
Release: 2018-06-12 17:00:42
Original
1509 people have browsed it

This article mainly introduces the method of realizing quick sorting in JavaScript, and analyzes the principles, implementation methods and related operation precautions of quick sorting in the form of examples. Friends in need can refer to the following

The examples in this article describe How to implement quick sort in JavaScript. Share it with everyone for your reference. The details are as follows:

Ideology:

Use the divide-and-conquer idea and recursive method to decompose the data into smaller ones in sequence. Different subsequences of small elements and larger elements

1. Select an element in the array as the base

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

3. Repeat the first two steps for the two subsets to the left and right of the benchmark until only one element remains 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<arr.length;i++){
 if(arr[i]<pivot){
 left.push(arr[i]);
}else{
 right.push(arr[i]);
}
}
return sqort(left).concat(pivot,qsort(right));//递归
}
var a=[];
for (i=0;i<10;++i){
a[i]=Math.floor(Math.random()*100+1);
}
console.log(a);
console.log(sqort(a));
//(基准以中间元素的情况)
function sqort(arr){
 if(arr.length<=1){
 return arr;
}
var left=[];
var right=[];
var pivotIndex=Math.floor(arr.length/2);
var pivot=arr.splice(pivotIndex,1)[0];//(基准以中间元素)
for(var i=1;i<arr.length;i++){
 if(arr[i]<pivot){
 left.push(arr[i]);
}else{
 right.push(arr[i]);
}
}
return sqort(left).concat(pivot,sqort(right));//递归
}
var a=[12,34,23,78,34,26];
console.log(a);
console.log(sqort(a));
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed interpretation of elements, components, instances and nodes in React

How to dynamically add data in AngularJS and delete?

What are the knowledge points of JS strict mode that will be explained in detail?

The above is the detailed content of How to implement quick sort using JavaScript (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!