列表

详情


FED4. 快速排序

描述

请补全JavaScript代码,要求将数组参数中的数字从小到大进行排序并返回该数组。
注意:
1. 数组元素仅包含数字
2. 请优先使用快速排序方法

示例1

输入:

_quickSort([0,-1,1,-2,2])

输出:

[-2,-1,0,1,2]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77824KB, 提交时间: 2021-12-07

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _quickSort = array => {\n                // 补全代码\n                array.sort(function(a,b){\n                    return a-b;\n                })\n                return array;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77772KB, 提交时间: 2021-12-07

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _quickSort = array => {\n                // 补全代码\n                return array.sort((a,b)=>{\n                    a-b\n                })\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77788KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _quickSort = array => {\n              if(array.length <= 1) return array\n    var pivotIndex = Math.floor(array.length / 2)\n    var pivot = array.splice(pivotIndex, 1)[0]\n    var left = []\n    var right = []\n    for (var i=0 ; i<array.length ; i++){\n        if (array[i] < pivot) {\n            left.push(array[i])\n        } else {\n            right.push(array[i])\n        }\n    }\n    return _quickSort(left).concat([pivot], _quickSort(right));\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77824KB, 提交时间: 2021-12-07

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _quickSort = array => {\n                // 补全代码\n                return array.sort((a,b)=>{\n                    return b-a\n                })\n                \n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77856KB, 提交时间: 2021-12-27

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _quickSort = array => {\n                // 补全代码\n                if(array.length === 0 || array.length === 1) return array; \n                let baseIndex = Math.floor(array.length / 2);\n                let base = array.splice(baseIndex,1);\n                let left = [], right = [];\n                for(let i=0;i<array.length;i++){\n                    if(array[i] <= base){\n                        left.push(array[i]);\n                    } else {\n                        right.push(array[i]);\n                    }\n                }\n                return _quickSort(left).concat(base,_quickSort(right));\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题