-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs__algorithm__sort__sort.md.js
More file actions
1 lines (1 loc) · 8.26 KB
/
docs__algorithm__sort__sort.md.js
File metadata and controls
1 lines (1 loc) · 8.26 KB
1
(window["webpackJsonp"]=window["webpackJsonp"]||[]).push([[15],{i4Qi:function(e,n,t){"use strict";t.r(n);var a=t("q1tI"),r=t.n(a),l=t("dEAq"),i=t("H1Ra"),c=r.a.memo((e=>{e.demos;return r.a.createElement(r.a.Fragment,null,r.a.createElement("div",{className:"markdown"},r.a.createElement("h2",{id:"\u5192\u6ce1\u6392\u5e8f"},r.a.createElement(l["AnchorLink"],{to:"#\u5192\u6ce1\u6392\u5e8f","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u5192\u6ce1\u6392\u5e8f"),r.a.createElement("p",null,"\u4ed6\u662f\u6700\u6162\u7684\u6392\u5e8f\u7b97\u6cd5\u4e4b\u4e00\uff0c\u6570\u636e\u503c\u4f1a\u50cf\u6c14\u6ce1\u4e00\u6837\u4ece\u6570\u7ec4\u7684\u4e00\u7aef\u6f02\u6d6e\u5230\u53e6\u4e00\u7aef"),r.a.createElement("h3",{id:"\u539f\u7406"},r.a.createElement(l["AnchorLink"],{to:"#\u539f\u7406","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u539f\u7406"),r.a.createElement("p",null,"\u6bd4\u8f83\u76f8\u90bb\u7684\u5143\u7d20\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u6bd4\u7b2c\u4e8c\u4e2a\u5927\u5c31\u4ea4\u6362\u4ed6\u4eec\u4e24\u4e2a\uff0c\u5143\u7d20\u5411\u4e0a\u79fb\u52a8\u81f3\u6b63\u786e\u7684\u987a\u5e8f\uff0c\u5c31\u597d\u50cf\u6c14\u6ce1\u5347\u5230\u8868\u9762\u4e00\u6837\uff0c",r.a.createElement("strong",null,"\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)")),r.a.createElement("h3",{id:"\u4ee3\u7801\u5b9e\u73b0"},r.a.createElement(l["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),r.a.createElement(i["a"],{code:"function bubbleSort(arr) {\n for (let i = 0; i < arr.length; i++) {\n for (let j = 0; j < arr.length - 1 - i; j++) {\n if (arr[j] > arr[j + 1]) {\n [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];\n }\n }\n }\n return arr;\n}\nconsole.log(bubbleSort([1, 3, 6, 2, 8, 9]));",lang:"js"}),r.a.createElement("h2",{id:"\u9009\u62e9\u6392\u5e8f"},r.a.createElement(l["AnchorLink"],{to:"#\u9009\u62e9\u6392\u5e8f","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u9009\u62e9\u6392\u5e8f"),r.a.createElement("h3",{id:"\u539f\u7406-1"},r.a.createElement(l["AnchorLink"],{to:"#\u539f\u7406-1","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u539f\u7406"),r.a.createElement("p",null,"\u4ece\u6570\u7ec4\u7684\u5f00\u5934\u5f00\u59cb\uff0c\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u548c\u5176\u4ed6\u5143\u7d20\u6bd4\u8f83\uff0c\u6700\u5c0f\u7684\u5143\u7d20\u4f1a\u88ab\u653e\u5230\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u518d\u4ece\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u7ee7\u7eed,",r.a.createElement("strong",null,"\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)")),r.a.createElement("h3",{id:"\u4ee3\u7801\u5b9e\u73b0-1"},r.a.createElement(l["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0-1","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),r.a.createElement(i["a"],{code:"function selectSort(arr) {\n let len = arr.length;\n let minIndex;\n for (let i = 0; i < len - 1; i++) {\n minIndex = i;\n for (j = i + 1; j < len; j++) {\n if (arr[j] < arr[minIndex]) {\n minIndex = j;\n }\n }\n [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];\n }\n return arr;\n}\n\nconsole.log('\u9009\u62e9', selectSort([1, 3, 6, 2, 8, 9]));",lang:"js"}),r.a.createElement("h2",{id:"\u63d2\u5165\u6392\u5e8f"},r.a.createElement(l["AnchorLink"],{to:"#\u63d2\u5165\u6392\u5e8f","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u63d2\u5165\u6392\u5e8f"),r.a.createElement("h3",{id:"\u539f\u7406-2"},r.a.createElement(l["AnchorLink"],{to:"#\u539f\u7406-2","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u539f\u7406"),r.a.createElement("p",null,"\u7c7b\u4f3c\u4e8e\u4eba\u4eec\u6309\u6570\u5b57\u6216\u5b57\u6bcd\u987a\u5e8f\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u540e\u9762\u7684\u8981\u4e3a\u524d\u9762\u817e\u4f4d\u7f6e"),r.a.createElement("h3",{id:"\u4ee3\u7801\u5b9e\u73b0-2"},r.a.createElement(l["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0-2","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),r.a.createElement(i["a"],{code:"function insertSort(arr) {\n let temp;\n for (let i = 1; i < arr.length; i++) {\n temp = arr[i];\n let j = i;\n console.log(j);\n while (j > 0 && arr[j - 1] > temp) {\n arr[j] = arr[j - 1];\n j--;\n }\n arr[j] = temp;\n }\n return arr;\n}\nconsole.log('\u63d2\u5165', insertSort([1, 3, 6, 2, 8, 9]));",lang:"js"}),r.a.createElement("h2",{id:"\u5feb\u901f\u6392\u5e8f"},r.a.createElement(l["AnchorLink"],{to:"#\u5feb\u901f\u6392\u5e8f","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u5feb\u901f\u6392\u5e8f"),r.a.createElement("h3",{id:"\u539f\u7406-3"},r.a.createElement(l["AnchorLink"],{to:"#\u539f\u7406-3","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u539f\u7406"),r.a.createElement("p",null,"\u5728\u5217\u8868\u4e2d\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u503c\uff0c\u6392\u5e8f\u56f4\u7ed5\u8fd9\u4e2a\u57fa\u51c6\u503c\u8fdb\u884c\uff0c\u5c06\u5217\u8868\u4e2d\u5c0f\u4e8e\u57fa\u51c6\u503c\u53d1\u653e\u5165\u6570\u7ec4\u5e95\u90e8\uff0c\u5927\u4e8e\u653e\u9876\u90e8\uff0c",r.a.createElement("strong",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(nlog(n))")),r.a.createElement("h3",{id:"\u4ee3\u7801\u5b9e\u73b0-3"},r.a.createElement(l["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0-3","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),r.a.createElement(i["a"],{code:"function quickSort(arr) {\n if (arr.length < 2) return arr;\n let privotIndex = Math.floor(arr.length / 2);\n let privot = arr.splice(privotIndex, 1)[0];\n let left = [],\n right = [];\n for (let i = 0; i < arr.length; i++) {\n if (arr[i] < privot) {\n left.push(arr[i]);\n } else {\n right.push(arr[i]);\n }\n }\n return quickSort(left).concat([privot], quickSort(right));\n}\nconsole.log('\u5feb\u6392', quickSort([1, 3, 6, 2, 8, 9]));",lang:"js"}),r.a.createElement("h2",{id:"\u5f52\u5e76\u6392\u5e8f"},r.a.createElement(l["AnchorLink"],{to:"#\u5f52\u5e76\u6392\u5e8f","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u5f52\u5e76\u6392\u5e8f"),r.a.createElement("h3",{id:"\u539f\u7406-4"},r.a.createElement(l["AnchorLink"],{to:"#\u539f\u7406-4","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u539f\u7406"),r.a.createElement("p",null,"\u628a\u4e00\u7cfb\u5217\u6392\u597d\u5e8f\u7684\u5b50\u5e8f\u5217\u5408\u5e76\u6210\u4e00\u4e2a\u5927\u7684\u5b8c\u6574\u6709\u5e8f\u5e8f\u5217\uff0c\u4e3b\u8981\u662f\u5206\u6cbb\u601d\u60f3\uff0c",r.a.createElement("strong",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(nlog(n))")),r.a.createElement("img",{src:t("yl6C")}),r.a.createElement("h3",{id:"\u4ee3\u7801\u5b9e\u73b0-4"},r.a.createElement(l["AnchorLink"],{to:"#\u4ee3\u7801\u5b9e\u73b0-4","aria-hidden":"true",tabIndex:-1},r.a.createElement("span",{className:"icon icon-link"})),"\u4ee3\u7801\u5b9e\u73b0"),r.a.createElement(i["a"],{code:"function mergeSort(arr) {\n if (arr.length < 2) return arr;\n let mid = Math.floor(arr.length / 2);\n let leftArr = arr.slice(0, mid);\n let rightArr = arr.slice(mid);\n return merge(mergeSort(leftArr), mergeSort(rightArr));\n}\nfunction merge(left, right) {\n var result = [];\n while (left.length && right.length) {\n if (left[0] < right[0]) {\n result.push(left.shift());\n } else {\n result.push(right.shift());\n }\n }\n while (left.length) {\n result.push(left.shift());\n }\n while (right.length) {\n result.push(right.shift());\n }\n return result;\n}\nconsole.log('\u5f52\u5e76', mergeSort([1, 3, 6, 2, 8, 9]));",lang:"js"})))}));n["default"]=e=>{var n=r.a.useContext(l["context"]),t=n.demos;return r.a.useEffect((()=>{var n;null!==e&&void 0!==e&&null!==(n=e.location)&&void 0!==n&&n.hash&&l["AnchorLink"].scrollToAnchor(decodeURIComponent(e.location.hash.slice(1)))}),[]),r.a.createElement(c,{demos:t})}},yl6C:function(e,n,t){e.exports=t.p+"static/\u5f52\u5e76\u6392\u5e8f.c20709ff.png"}}]);