{"id":5446,"date":"2023-01-05T12:41:30","date_gmt":"2023-01-05T04:41:30","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5446"},"modified":"2023-01-05T12:41:30","modified_gmt":"2023-01-05T04:41:30","slug":"%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8fquicksort","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e5%bf%ab%e9%80%9f%e6%8e%92%e5%ba%8fquicksort\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u4e2d\u7684\u5feb\u901f\u6392\u5e8f(quickSort)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u5feb\u901f\u6392\u5e8f, quickSort\"><\/p>\n<h1>TypeScript \u9663\u5217\u7684\u5feb\u901f\u6392\u5e8f(quickSort)<\/h1>\n<p>\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5b83\u662f\u7531C. A. R. Hoare\u57281962\u5e74\u63d0\u51fa\u7684\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\uff1a\u9078\u64c7\u4e00\u500b\u57fa\u6e96\u503c\uff0c\u901a\u5e38\u662f\u6578\u7d44\u7684\u7b2c\u4e00\u500b\u5143\u7d20\uff0c\u7136\u5f8c\u5c07\u6578\u7d44\u5206\u6210\u5169\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4\u57fa\u6e96\u503c\u5c0f\uff0c\u53e6\u4e00\u90e8\u5206\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4\u57fa\u6e96\u503c\u5927\uff0c\u7136\u5f8c\u5c0d\u5169\u90e8\u5206\u5206\u5225\u905e\u6b78\u5730\u9032\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u76f4\u5230\u6578\u7d44\u7684\u6240\u6709\u5143\u7d20\u90fd\u6709\u5e8f\u70ba\u6b62\u3002<\/p>\n<p>TypeScript\u4e2d\u7684\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5be6\u73fe\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction quickSort(arr: number[], left: number, right: number): void {\n    let i: number = left;\n    let j: number = right;\n    let temp: number;\n    let pivot: number = arr[(left + right) \/ 2];\n\n    while (i <= j) {\n        while (arr[i] < pivot) {\n            i++;\n        }\n        while (arr[j] > pivot) {\n            j--;\n        }\n        if (i <= j) {\n            temp = arr[i];\n            arr[i] = arr[j];\n            arr[j] = temp;\n            i++;\n            j--;\n        }\n    }\n\n    if (left < j) {\n        quickSort(arr, left, j);\n    }\n    if (i < right) {\n        quickSort(arr, i, right);\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u4e00\u500b\u57fa\u6e96\u503c\uff0c\u5b83\u662f\u6578\u7d44\u7684\u4e2d\u9593\u5143\u7d20\uff0c\u7136\u5f8c\u5c07\u6578\u7d44\u5206\u6210\u5169\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4\u57fa\u6e96\u503c\u5c0f\uff0c\u53e6\u4e00\u90e8\u5206\u7684\u6240\u6709\u5143\u7d20\u90fd\u6bd4\u57fa\u6e96\u503c\u5927\u3002\u7136\u5f8c\u5c0d\u5169\u90e8\u5206\u5206\u5225\u905e\u6b78\u5730\u9032\u884c\u5feb\u901f\u6392\u5e8f\uff0c\u76f4\u5230\u6578\u7d44\u7684\u6240\u6709\u5143\u7d20\u90fd\u6709\u5e8f\u70ba\u6b62\u3002<\/p>\n<p>\u5feb\u901f\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662fO(nlogn)\uff0c\u5b83\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u53ef\u4ee5\u7528\u65bc\u5927\u91cf\u6578\u64da\u7684\u6392\u5e8f\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5feb\u901f\u6392\u5e8f\u7684\u512a\u9ede\u662f\u5b83\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4f4e\uff0c\u5b83\u53ef\u4ee5\u5728O(nlogn)\u7684\u6642\u9593\u5167\u5b8c\u6210\u6392\u5e8f\uff0c\u800c\u4e14\u5b83\u4e0d\u9700\u8981\u984d\u5916\u7684\u5167\u5b58\u7a7a\u9593\uff0c\u5b83\u662f\u4e00\u7a2e\u539f\u5730\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5feb\u901f\u6392\u5e8f\u4e5f\u6709\u4e00\u4e9b\u7f3a\u9ede\uff0c\u5b83\u7684\u6642\u9593\u8907\u96dc\u5ea6\u53d6\u6c7a\u65bc\u57fa\u6e96\u503c\u7684\u9078\u64c7\uff0c\u5982\u679c\u57fa\u6e96\u503c\u4e0d\u9069\u7576\uff0c\u5b83\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5c31\u6703\u8b8a\u5f97\u5f88\u9ad8\uff0c\u53ef\u80fd\u6703\u8b8a\u6210O(n<sup>2<\/sup>)\u3002\u53e6\u5916\uff0c\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u7a2e\u4e0d\u7a69\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u6703\u6539\u8b8a\u76f8\u540c\u5143\u7d20\u7684\u76f8\u5c0d\u9806\u5e8f\u3002<\/p>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0c\u5feb\u901f\u6392\u5e8f\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5728O(nlogn)\u7684\u6642\u9593\u5167\u5b8c\u6210\u6392\u5e8f\uff0c\u800c\u4e14\u5b83\u4e0d\u9700\u8981\u984d\u5916\u7684\u5167\u5b58\u7a7a\u9593\uff0c\u5b83\u662f\u4e00\u7a2e\u539f\u5730\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aTypeScript\u4e2d\u7684\u5feb\u901f\u6392\u5e8f(quickSort)\u662f\u4e00\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u5730\u5c07\u9663\u5217\u4e2d\u7684\u5143\u7d20\u6392\u5e8f\uff0c\u4e26\u4e14\u6bd4\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\u66f4\u6709\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5feb\u901f\u6392\u5e8f\u7684\u57fa\u672c\u539f\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u5728TypeScript\u4e2d\u5be6\u73fe\u5feb\u901f\u6392\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5446","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1pQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5446"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5446\/revisions"}],"predecessor-version":[{"id":5447,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5446\/revisions\/5447"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}