{"id":5434,"date":"2023-01-05T12:40:14","date_gmt":"2023-01-05T04:40:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5434"},"modified":"2023-01-05T12:40:14","modified_gmt":"2023-01-05T04:40:14","slug":"%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e9%80%89%e6%8b%a9%e6%8e%92%e5%ba%8f%ef%bc%88selectionsort%ef%bc%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e9%80%89%e6%8b%a9%e6%8e%92%e5%ba%8f%ef%bc%88selectionsort%ef%bc%89\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u4e2d\u7684\u9009\u62e9\u6392\u5e8f\uff08SelectionSort\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u9078\u64c7\u6392\u5e8f, selectionSort\"><\/p>\n<h1>TypeScript \u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f(selectionSort)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e\u7531\u5fae\u8edf\u958b\u767c\u7684 JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709\u66f4\u5f37\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u958b\u767c\u51fa\u66f4\u9ad8\u54c1\u8cea\u7684\u7a0b\u5f0f\u78bc\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9078\u64c7\u6392\u5e8f(selectionSort)\u4f86\u5c0d\u9663\u5217\u9032\u884c\u6392\u5e8f\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u9078\u64c7\u6392\u5e8f\uff1f<\/h2>\n<p>\u9078\u64c7\u6392\u5e8f\u662f\u4e00\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5b83\u7684\u539f\u7406\u662f\u5f9e\u9663\u5217\u4e2d\u627e\u51fa\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u4e26\u5c07\u5176\u8207\u9663\u5217\u7684\u7b2c\u4e00\u500b\u5143\u7d20\u4ea4\u63db\u4f4d\u7f6e\uff0c\u7136\u5f8c\u518d\u5f9e\u5269\u4e0b\u7684\u5143\u7d20\u4e2d\u627e\u51fa\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u4e26\u5c07\u5176\u8207\u9663\u5217\u7684\u7b2c\u4e8c\u500b\u5143\u7d20\u4ea4\u63db\u4f4d\u7f6e\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u76f4\u5230\u5c07\u6574\u500b\u9663\u5217\u6392\u5e8f\u5b8c\u6210\u3002<\/p>\n<h2>TypeScript \u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 TypeScript \u5be6\u73fe\u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction selectionSort(arr: number[]) {\n    let minIndex: number;\n    let temp: number;\n    for (let i = 0; i < arr.length - 1; i++) {\n        minIndex = i;\n        for (let j = i + 1; j < arr.length; j++) {\n            if (arr[j] < arr[minIndex]) {\n                minIndex = j;\n            }\n        }\n        temp = arr[i];\n        arr[i] = arr[minIndex];\n        arr[minIndex] = temp;\n    }\n    return arr;\n}\n\nlet arr: number[] = [3, 5, 1, 4, 2];\nconsole.log(selectionSort(arr)); \/\/ [1, 2, 3, 4, 5]\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>selectionSort<\/code> \u7684\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b <code>number<\/code> \u985e\u578b\u7684\u9663\u5217\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u5c0d\u9663\u5217\u4e2d\u7684\u5143\u7d20\u9032\u884c\u9078\u64c7\u6392\u5e8f\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b <code>minIndex<\/code> \u8b8a\u6578\uff0c\u7528\u65bc\u8a18\u9304\u7576\u524d\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u4e00\u500b <code>temp<\/code> \u8b8a\u6578\uff0c\u7528\u65bc\u4ea4\u63db\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528\u4e00\u500b\u5916\u5c64\u7684 <code>for<\/code> \u5faa\u74b0\u4f86\u904d\u6b77\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\uff0c\u4e26\u4f7f\u7528\u4e00\u500b\u5167\u5c64\u7684 <code>for<\/code> \u5faa\u74b0\u4f86\u5c0b\u627e\u7576\u524d\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u6700\u5f8c\u5c07\u7576\u524d\u6700\u5c0f\u7684\u5143\u7d20\u8207\u5916\u5c64 <code>for<\/code> \u5faa\u74b0\u4e2d\u7684\u5143\u7d20\u4ea4\u63db\u4f4d\u7f6e\uff0c\u76f4\u5230\u5c07\u6574\u500b\u9663\u5217\u6392\u5e8f\u5b8c\u6210\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b <code>number<\/code> \u985e\u578b\u7684\u9663\u5217 <code>arr<\/code>\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66 <code>selectionSort<\/code> \u51fd\u6578\uff0c\u6700\u5f8c\u5c07\u6392\u5e8f\u5f8c\u7684\u9663\u5217\u8f38\u51fa\u5230\u63a7\u5236\u53f0\uff0c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: typescript\">\n[1, 2, 3, 4, 5]\n<\/pre>\n<p>\u5f9e\u4e0a\u9762\u7684\u7bc4\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u4f7f\u7528 TypeScript \u4f86\u5be6\u73fe\u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f\u662f\u975e\u5e38\u7c21\u55ae\u7684\uff0c\u53ea\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u4e26\u4f7f\u7528\u5169\u500b <code>for<\/code> \u5faa\u74b0\u4f86\u5c0b\u627e\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c31\u53ef\u4ee5\u5c07\u9663\u5217\u6392\u5e8f\u5b8c\u6210\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86 TypeScript \u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4f7f\u7528 TypeScript \u4f86\u5be6\u73fe\u9663\u5217\u7684\u9078\u64c7\u6392\u5e8f\u3002\u96d6\u7136\u9078\u64c7\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f <span style=\"color: #f00; font-weight: bold;\">O(n<sup>2<\/sup>)<\/span>\uff0c\u4f46\u5b83\u7684\u512a\u52e2\u5728\u65bc\u5b83\u7684\u7a69\u5b9a\u6027\uff0c\u53ef\u4ee5\u4fdd\u8b49\u6392\u5e8f\u5f8c\u7684\u9663\u5217\u4e2d\u76f8\u540c\u5143\u7d20\u7684\u76f8\u5c0d\u4f4d\u7f6e\u4e0d\u6703\u6539\u8b8a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2TypeScript\u4e2d\u7684\u9009\u62e9\u6392\u5e8f\uff08SelectionSort\uff09\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c0d\u9663\u5217\u4e2d\u7684\u5143\u7d20\u9032\u884c\u6392\u5e8f\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u9009\u62e9\u6392\u5e8f\uff08SelectionSort\uff09\u4f86\u5be6\u73fe\u9663\u5217\u7684\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-5434","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-1pE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5434","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=5434"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5434\/revisions"}],"predecessor-version":[{"id":5435,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5434\/revisions\/5435"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}