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

Js quick sort method example

小云云
Release: 2018-02-26 13:55:09
Original
2249 people have browsed it


Quick sorting is mainly divided into three parts: 1. Select a pivot (pivot) 2. All elements smaller than the pivot value are placed in front of the pivot, and all elements larger than the pivot value are placed in front of the pivot. Place behind the base (the same number can go to either side). After this partition exits, the base is in the middle of the sequence. This is called a partition operation; 3. Recursively sort the subarray of elements smaller than the base value and the subarray of elements greater than the base value; the bottom case of recursion is when the size of the array is zero or one, that is, It's just that it's always been sorted. Although it continues to recurse, this algorithm will always exit, because in each iteration (iteration), it will move at least one element to its final position.

function quickSort(arr) {   
if (arr.length <= 1) { return arr } console.log("原数组是:" + arr)   
var pivotIndex = Math.floor(arr.length / 2)   
var pivot = arr.splice(pivotIndex, 1)[0]  
var left = []   
var right = []
 console.log("将中介提取出来后数组是:" + arr)   
for (var i = 0 ; i < arr.length ; i++){ 
console.log("此刻中介是:" + pivot + "当前元素是:" + arr[i])    
 if (arr[i] < pivot) {      
 left.push(arr[i]) console.log("移动" + arr[i] + "到左边")    
 } else {      
 right.push(arr[i]) console.log("移动" + arr[i] + "到右边")    
 }  
 }  
return quickSort(left).concat([pivot], quickSort(right)) } 
var nums = [2,3,4,3,1,5,7,122,341,-1] 
console.log(quickSort(nums))
Copy after login
Copy after login

Second method:

function quickSort(arr) {   
if (arr.length <= 1) { return arr } console.log("原数组是:" + arr)   
var pivotIndex = Math.floor(arr.length / 2)   
var pivot = arr.splice(pivotIndex, 1)[0]  
var left = []   
var right = []
 console.log("将中介提取出来后数组是:" + arr)   
for (var i = 0 ; i < arr.length ; i++){ 
console.log("此刻中介是:" + pivot + "当前元素是:" + arr[i])    
 if (arr[i] < pivot) {      
 left.push(arr[i]) console.log("移动" + arr[i] + "到左边")    
 } else {      
 right.push(arr[i]) console.log("移动" + arr[i] + "到右边")    
 }  
 }  
return quickSort(left).concat([pivot], quickSort(right)) } 
var nums = [2,3,4,3,1,5,7,122,341,-1] 
console.log(quickSort(nums))
Copy after login
Copy after login

Related recommendations:

JavaScript to implement quick sort analysis

PHP Example of Implementing Quick Sorting Method

#php Example of Implementing Quick Sorting Algorithm for Two-Dimensional Arrays

The above is the detailed content of Js quick sort method example. 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!