11 <script> 12 function quickSort(arr) { 13 if (arr.length <= 1) { 14 return arr; 15 } 16 17 var num = Math.floor(arr.length/2); 18 19 var numValue = arr.splice(num,1); 20 var left = []; 21 var right = []; 22 23 for (var i = 0; i < arr.length; i++) { 24 if (arr[i] < numValue) { 25 left.push(arr[i]); 26 } 27 else { 28 right.push(arr[i]); 29 } 30 } 31 32 return quickSort(left).concat([numValue], quickSort(right)); 33 } 34 alert(quickSort([32,45,37,16,1,87])) 35 </script>
时间: 2024-10-10 17:48:16