标签:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script type="text/javascript"> function quickSort(arr){ if(arr.length<=1){ return arr; } var num = Math.floor( arr.length/2 ); var numValue = arr.splice(num,1); var left = []; var right = []; for(var i=0;i<arr.length;i++){ if(arr[i]<numValue){ left.push(arr[i]); }else{ right.push(arr[i]); } } return quickSort(left).concat([numValue],quickSort(right)); } alert( quickSort([22,30,15,7,9,44,18]) ); </script> </head> <body> </body> </html>
原理就是找到数组的中心点作为基点,把比基点小的都放在左边,大的都放在右边,然后再利用递归即可。
标签:
原文地址:http://www.cnblogs.com/toodeep/p/4474304.html