标签:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> window.onload = function (){ var arr = [34,7,3,11,20,40,13,5]; function quickSort(arr){ if(arr.length <= 1){ return arr; } var num = arr[Math.ceil(arr.length/2 - 1) ]; var arrL = []; var arrC = arr.splice(num,1); var arrR = []; for(var i = 0; i < arr.length; i++){ if(arr[i] < num){ arrL.push( arr[i] ); } else if( arr[i] > num ){ arrR.push( arr[i] ) }else{ arrC.push(arr[i]); } } return quickSort( arrL ).concat( arrC , quickSort( arrR ) ); } console.log(quickSort( arr )); } </script> </head> <body> </body> </html>
标签:
原文地址:http://www.cnblogs.com/mayufo/p/4458519.html