1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <script> function setRandomArray(){ return Array.from({length:10},item=>parseInt(Math.random() * 100)) } let arr1 = setRandomArray(); console.log(arr1); let arr2 = orderData(arr1.concat()); console.log(arr2); function orderData(arr){ let left=[],right=[]; let current = arr[0]; if(arr.length <=1){ return arr; } for(let i=1,length = arr.length;i<length;i++){ if(arr[i] > current){ right.push(arr[i]); } else{ left.push(arr[i]) } } return [...orderData(left),current,...orderData(right)] } </script>
</body> </html>
|