{"id":1502,"date":"2025-09-28T11:05:58","date_gmt":"2025-09-28T04:05:58","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1502"},"modified":"2025-11-05T23:35:53","modified_gmt":"2025-11-05T16:35:53","slug":"sap-xep-cac-phan-tu-trong-array-voi-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/sap-xep-cac-phan-tu-trong-array-voi-python\/","title":{"rendered":"S\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed trong Array v\u1edbi Python"},"content":{"rendered":"\n<p>Array (m\u1ea3ng) l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u n\u1ec1n t\u1ea3ng trong l\u1eadp tr\u00ecnh \u2014 l\u01b0u tr\u1eef nhi\u1ec1u ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1, cho ph\u00e9p truy c\u1eadp nhanh theo ch\u1ec9 s\u1ed1. Vi\u1ec7c <strong>s\u1eafp x\u1ebfp<\/strong> c\u00e1c ph\u1ea7n t\u1eed trong Array l\u00e0 thao t\u00e1c c\u1ef1c k\u1ef3 ph\u1ed5 bi\u1ebfn v\u00e0 quan tr\u1ecdng: gi\u00fap t\u00ecm ki\u1ebfm nhanh h\u01a1n (v\u00ed d\u1ee5 binary search), d\u1ec5 ph\u00e2n t\u00edch hay tr\u00ecnh b\u00e0y d\u1eef li\u1ec7u (v\u00ed d\u1ee5 s\u1eafp x\u1ebfp theo \u0111i\u1ec3m, theo t\u00ean). Trong b\u00e0i n\u00e0y, m\u00ecnh s\u1ebd \u0111i t\u1eeb t\u1ed5ng quan \u0111\u1ebfn th\u1ef1c h\u00e0nh: d\u00f9ng h\u00e0m c\u00f3 s\u1eb5n v\u00e0 t\u1ef1 hi\u1ec7n th\u1ef1c c\u00e1c thu\u1eadt to\u00e1n, ph\u00e2n t\u00edch \u0111\u1ed9 ph\u1ee9c t\u1ea1p v\u00e0 n\u00eau nh\u1eefng l\u01b0u \u00fd th\u1ef1c t\u1ebf.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. T\u1ed5ng quan v\u1ec1 Array v\u00e0 Sorting<\/h2>\n\n\n\n<p>Array l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c ph\u1ea7n t\u1eed l\u01b0u li\u00ean ti\u1ebfp trong b\u1ed9 nh\u1edb (\u1edf nhi\u1ec1u ng\u00f4n ng\u1eef). Vi\u1ec7c s\u1eafp x\u1ebfp (sorting) \u0111\u01b0a c\u00e1c ph\u1ea7n t\u1eed v\u1ec1 m\u1ed9t th\u1ee9 t\u1ef1 x\u00e1c \u0111\u1ecbnh \u2014 th\u01b0\u1eddng l\u00e0 <strong>t\u0103ng d\u1ea7n<\/strong> ho\u1eb7c <strong>gi\u1ea3m d\u1ea7n<\/strong>, nh\u01b0ng c\u0169ng c\u00f3 th\u1ec3 l\u00e0 theo ti\u00eau ch\u00ed t\u00f9y bi\u1ebfn (v\u00ed d\u1ee5: \u0111\u1ed9 d\u00e0i chu\u1ed7i, s\u1ed1 \u0111i\u1ec3m).<\/p>\n\n\n\n<p>T\u1ea1i sao c\u1ea7n s\u1eafp x\u1ebfp?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T\u0103ng t\u1ed1c t\u00ecm ki\u1ebfm (binary search y\u00eau c\u1ea7u d\u1eef li\u1ec7u \u0111\u00e3 s\u1eafp).<\/li>\n\n\n\n<li>Chu\u1ea9n h\u00f3a hi\u1ec3n th\u1ecb (danh s\u00e1ch theo t\u00ean, theo \u0111i\u1ec3m).<\/li>\n\n\n\n<li>H\u1ed7 tr\u1ee3 c\u00e1c thu\u1eadt to\u00e1n kh\u00e1c (merge step trong merge sort, k\u1ef9 thu\u1eadt two-pointer, &#8230;).<\/li>\n<\/ul>\n\n\n\n<p>Lo\u1ea1i s\u1eafp x\u1ebfp c\u01a1 b\u1ea3n:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In-place (s\u1eafp ngay tr\u00ean m\u1ea3ng, kh\u00f4ng c\u1ea7n b\u1ed9 nh\u1edb ph\u1ee5 l\u1edbn).<\/li>\n\n\n\n<li>Not in-place (tr\u1ea3 v\u1ec1 m\u1ea3ng m\u1edbi).<\/li>\n\n\n\n<li>Stable (gi\u1eef th\u1ee9 t\u1ef1 t\u01b0\u01a1ng \u0111\u1ed1i c\u1ee7a ph\u1ea7n t\u1eed b\u1eb1ng nhau) vs Unstable.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. S\u1eafp x\u1ebfp Array b\u1eb1ng h\u00e0m d\u1ef1ng s\u1eb5n<\/h2>\n\n\n\n<p>Trong Python c\u00f3 hai c\u00e1ch ch\u00ednh: <code>sorted()<\/code> (tr\u1ea3 v\u1ec1 list m\u1edbi) v\u00e0 <code>list.sort()<\/code> (s\u1eafp x\u1ebfp t\u1ea1i ch\u1ed7).<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 v\u00e0 gi\u1ea3i th\u00edch (code comments b\u1eb1ng ti\u1ebfng Anh):<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>numbers = &#91;5, 2, 9, 1, 5&#93;\n\n# Returns a new sorted list\nsorted_numbers = sorted(numbers)  # returns a new sorted list\n\n# Sorts the list in-place (no new list created)\nnumbers.sort()  # sorts the list in-place\n\n# Sorting with a custom key: sort strings by length\nwords = &#91;\"apple\", \"fig\", \"banana\"&#93;\nwords.sort(key=len)  # sorts by the length of each string\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">9<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Returns a new sorted list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># returns a new sorted list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sorts the list in-place (no new list created)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sort<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># sorts the list in-place<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sorting with a custom key: sort strings by length<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">words <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">apple<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">fig<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">banana<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">words<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">key<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># sorts by the length of each string<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Gi\u1ea3i th\u00edch chi ti\u1ebft:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">sorted()<\/mark><\/code> tr\u1ea3 v\u1ec1 m\u1ed9t danh s\u00e1ch m\u1edbi, ph\u00f9 h\u1ee3p khi b\u1ea1n c\u1ea7n gi\u1eef m\u1ea3ng g\u1ed1c.<\/li>\n\n\n\n<li><code><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">list.sort()<\/mark><\/code> nhanh h\u01a1n ch\u00fat v\u00ec l\u00e0m tr\u1ef1c ti\u1ebfp (in-place) v\u00e0 kh\u00f4ng t\u1ea1o b\u1ea3n sao.<\/li>\n\n\n\n<li>C\u1ea3 hai ch\u1ea5p nh\u1eadn tham s\u1ed1 <code>key=<\/code> \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a ti\u00eau ch\u00ed s\u1eafp x\u1ebfp (v\u00ed d\u1ee5 <code>key=len<\/code> ho\u1eb7c <code>key=lambda x: x['score']<\/code>) v\u00e0 <code>reverse=True<\/code> \u0111\u1ec3 s\u1eafp gi\u1ea3m d\u1ea7n.<\/li>\n\n\n\n<li>Python d\u00f9ng thu\u1eadt to\u00e1n <strong>Timsort<\/strong> (k\u1ebft h\u1ee3p insertion + merge), hi\u1ec7u qu\u1ea3 cho d\u1eef li\u1ec7u th\u1ef1c t\u1ebf \u2014 O(n log n) trung b\u00ecnh, stable.<\/li>\n<\/ul>\n\n\n\n<p>\u01afu \u0111i\u1ec3m c\u1ee7a h\u00e0m d\u1ef1ng s\u1eb5n: nhanh, an to\u00e0n, chu\u1ea9n; nh\u01b0\u1ee3c \u0111i\u1ec3m: \u00edt ki\u1ec3m so\u00e1t chi ti\u1ebft thu\u1eadt to\u00e1n (nh\u01b0ng \u0111i\u1ec1u n\u00e0y hi\u1ebfm khi c\u1ea7n).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. C\u00e1c thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u1ed5 bi\u1ebfn<\/h2>\n\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y m\u00ecnh tri\u1ec3n khai b\u1ed1n thu\u1eadt to\u00e1n ti\u00eau bi\u1ec3u, c\u00f3 code Python (comment &amp; bi\u1ebfn b\u1eb1ng English) v\u00e0 gi\u1ea3i th\u00edch chi ti\u1ebft t\u1eebng b\u01b0\u1edbc, \u0111\u1ed9 ph\u1ee9c t\u1ea1p, khi n\u00e0o h\u1ee3p l\u00fd d\u00f9ng.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Bubble Sort<\/h3>\n\n\n\n<p><strong>\u00dd t\u01b0\u1edfng:<\/strong> L\u1eb7p qua m\u1ea3ng, so s\u00e1nh t\u1eebng c\u1eb7p k\u1ebf nhau v\u00e0 ho\u00e1n \u0111\u1ed5i n\u1ebfu sai th\u1ee9 t\u1ef1; l\u1eb7p cho \u0111\u1ebfn khi kh\u00f4ng c\u00f2n ho\u00e1n \u0111\u1ed5i.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def bubble_sort(arr):\n    n = len(arr)\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            # If the element found is greater than the next element, swap them\n            if arr&#91;j&#93; > arr&#91;j + 1&#93;:\n                arr&#91;j&#93;, arr&#91;j + 1&#93; = arr&#91;j + 1&#93;, arr&#91;j&#93;\n                swapped = True\n        # If no two elements were swapped by inner loop, then break\n        if not swapped:\n            break\n    return arr\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">bubble_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">arr<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    n <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">arr<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">n<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        swapped <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> j <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> n <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #616E88\"># If the element found is greater than the next element, swap them<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                swapped <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># If no two elements were swapped by inner loop, then break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">not<\/span><span style=\"color: #D8DEE9FF\"> swapped<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch chi ti\u1ebft:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>V\u00f2ng ngo\u00e0i ch\u1ea1y <code>n<\/code> l\u1ea7n, v\u00f2ng trong \u0111\u01b0a ph\u1ea7n t\u1eed l\u1edbn nh\u1ea5t c\u00f2n l\u1ea1i v\u1ec1 cu\u1ed1i.<\/li>\n\n\n\n<li><code>swapped<\/code> gi\u00fap t\u1ed1i \u01b0u: n\u1ebfu v\u00f2ng trong kh\u00f4ng ho\u00e1n \u0111\u1ed5i n\u00e0o \u2014 m\u1ea3ng \u0111\u00e3 s\u1eafp \u2014 ta d\u1eebng s\u1edbm.<\/li>\n\n\n\n<li>\u0110\u1ed9 ph\u1ee9c t\u1ea1p: O(n\u00b2) th\u1eddi gian (t\u1ec7 nh\u1ea5t &amp; trung b\u00ecnh), O(1) kh\u00f4ng gian.<\/li>\n\n\n\n<li>Khi d\u00f9ng? Ch\u1ec9 d\u00f9ng \u0111\u1ec3 h\u1ecdc thu\u1eadt, demo v\u00ec \u0111\u01a1n gi\u1ea3n; kh\u00f4ng d\u00f9ng cho d\u1eef li\u1ec7u l\u1edbn.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 Selection Sort<\/h3>\n\n\n\n<p><strong>\u00dd t\u01b0\u1edfng:<\/strong> V\u1edbi m\u1ed7i v\u1ecb tr\u00ed i, t\u00ecm ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t t\u1eeb i..n-1 v\u00e0 \u0111\u1ed5i ch\u1ed7 v\u00e0o v\u1ecb tr\u00ed i.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def selection_sort(arr):\n    n = len(arr)\n    for i in range(n):\n        min_index = i\n        for j in range(i + 1, n):\n            if arr&#91;j&#93; &lt; arr&#91;min_index&#93;:\n                min_index = j\n        # Swap the found minimum element with the first element\n        arr&#91;i&#93;, arr&#91;min_index&#93; = arr&#91;min_index&#93;, arr&#91;i&#93;\n    return arr\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">selection_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">arr<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    n <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">arr<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">n<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        min_index <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> j <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">i <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> n<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">min_index<\/span><span style=\"color: #ECEFF4\">&#93;:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                min_index <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> j<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># Swap the found minimum element with the first element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">min_index<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">min_index<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>M\u1ed7i l\u1ea7n ch\u1ecdn ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t v\u00e0 \u0111\u1eb7t v\u00e0o v\u1ecb tr\u00ed ti\u1ebfp theo.<\/li>\n\n\n\n<li>\u0110\u1ed9 ph\u1ee9c t\u1ea1p: O(n\u00b2), kh\u00f4ng gian O(1).<\/li>\n\n\n\n<li>Kh\u00f4ng stable n\u1ebfu tri\u1ec3n khai ho\u00e1n \u0111\u1ed5i tr\u1ef1c ti\u1ebfp (c\u00f3 c\u00e1ch stable h\u01a1n nh\u01b0ng ph\u1ee9c t\u1ea1p h\u01a1n).<\/li>\n\n\n\n<li>Th\u01b0\u1eddng d\u00f9ng cho h\u1ecdc thu\u1eadt ho\u1eb7c khi b\u1ed9 nh\u1edb ph\u1ee5 b\u1ecb h\u1ea1n ch\u1ebf nh\u01b0ng v\u1eabn kh\u00f4ng ph\u1ea3i l\u1ef1a ch\u1ecdn t\u1ed1t cho n l\u1edbn.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 Insertion Sort<\/h3>\n\n\n\n<p><strong>\u00dd t\u01b0\u1edfng:<\/strong> X\u00e2y d\u00e3y \u0111\u00e3 s\u1eafp x\u1ebfp t\u1eeb \u0111\u1ea7u, l\u1ea7n l\u01b0\u1ee3t ch\u00e8n t\u1eebng ph\u1ea7n t\u1eed m\u1edbi v\u00e0o v\u1ecb tr\u00ed ph\u00f9 h\u1ee3p.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def insertion_sort(arr):\n    for i in range(1, len(arr)):\n        key = arr&#91;i&#93;\n        j = i - 1\n        # Move elements of arr&#91;0..i-1&#93; that are greater than key to one position ahead\n        while j >= 0 and arr&#91;j&#93; > key:\n            arr&#91;j + 1&#93; = arr&#91;j&#93;\n            j -= 1\n        arr&#91;j + 1&#93; = key\n    return arr\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">insertion_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">arr<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">arr<\/span><span style=\"color: #ECEFF4\">)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        key <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        j <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> i <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># Move elements of arr&#91;0..i-1&#93; that are greater than key to one position ahead<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">while<\/span><span style=\"color: #D8DEE9FF\"> j <\/span><span style=\"color: #81A1C1\">&gt;=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">and<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> key<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            j <\/span><span style=\"color: #81A1C1\">-=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> key<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hi\u1ec7u qu\u1ea3 v\u1edbi m\u1ea3ng g\u1ea7n nh\u01b0 \u0111\u00e3 s\u1eafp x\u1ebfp: th\u1eddi gian g\u1ea7n O(n).<\/li>\n\n\n\n<li>\u0110\u1ed9 ph\u1ee9c t\u1ea1p trung b\u00ecnh &amp; t\u1ec7 nh\u1ea5t O(n\u00b2), b\u1ed9 nh\u1edb O(1).<\/li>\n\n\n\n<li>Stable (b\u1ea3o to\u00e0n th\u1ee9 t\u1ef1 t\u01b0\u01a1ng \u0111\u1ed1i ph\u1ea7n t\u1eed b\u1eb1ng nhau).<\/li>\n\n\n\n<li>Th\u01b0\u1eddng d\u00f9ng cho m\u1ea3ng nh\u1ecf ho\u1eb7c nh\u01b0 m\u1ed9t b\u01b0\u1edbc trong thu\u1eadt to\u00e1n ph\u1ee9c t\u1ea1p h\u01a1n (v\u00ed d\u1ee5 trong Timsort).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 Quick Sort<\/h3>\n\n\n\n<p><strong>\u00dd t\u01b0\u1edfng:<\/strong> Ch\u1ecdn m\u1ed9t pivot, chia m\u1ea3ng th\u00e0nh hai ph\u1ea7n (&lt; pivot v\u00e0 &gt;= pivot), \u0111\u1ec7 quy s\u1eafp x\u1ebfp hai ph\u1ea7n r\u1ed3i gh\u00e9p l\u1ea1i.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>def quick_sort(arr):\n    # Base case\n    if len(arr) &lt;= 1:\n        return arr\n    pivot = arr&#91;len(arr) \/\/ 2&#93;\n    left = &#91;x for x in arr if x &lt; pivot&#93;\n    middle = &#91;x for x in arr if x == pivot&#93;\n    right = &#91;x for x in arr if x > pivot&#93;\n    # Recursively sort left and right, then concatenate\n    return quick_sort(left) + middle + quick_sort(right)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">quick_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">arr<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\"># Base case<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">arr<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">&lt;=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    pivot <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> arr<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">arr<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">\/\/<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    left <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> arr <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\"> pivot<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    middle <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> arr <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> pivot<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    right <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> arr <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> pivot<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\"># Recursively sort left and right, then concatenate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">quick_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">left<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> middle <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">quick_sort<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">right<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch chi ti\u1ebft:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u00e1ch tri\u1ec3n khai tr\u00ean l\u00e0 version kh\u00f4ng in-place, d\u1ec5 hi\u1ec3u (s\u1eed d\u1ee5ng list comprehensions).<\/li>\n\n\n\n<li>\u0110\u1ed9 ph\u1ee9c t\u1ea1p trung b\u00ecnh O(n log n), t\u1ec7 nh\u1ea5t O(n\u00b2) (khi pivot t\u1ec7 \u2014 v\u00ed d\u1ee5 khi m\u1ea3ng \u0111\u00e3 s\u1eafp theo th\u1ee9 t\u1ef1 v\u00e0 pivot lu\u00f4n l\u00e0 ph\u1ea7n t\u1eed c\u1ef1c \u0111oan).<\/li>\n\n\n\n<li>Kh\u00f4ng stable theo tri\u1ec3n khai n\u00e0y; c\u00f3 th\u1ec3 l\u00e0m in-place v\u1edbi partition (Lomuto\/Hoare) \u0111\u1ec3 gi\u1ea3m b\u1ed9 nh\u1edb.<\/li>\n\n\n\n<li>Th\u01b0\u1eddng ch\u1ecdn pivot ng\u1eabu nhi\u00ean ho\u1eb7c median-of-three \u0111\u1ec3 c\u1ea3i thi\u1ec7n tr\u01b0\u1eddng h\u1ee3p x\u1ea5u.<\/li>\n\n\n\n<li>Quick Sort r\u1ea5t ph\u1ed5 bi\u1ebfn nh\u1edd hi\u1ec7u n\u0103ng t\u1ed1t trung b\u00ecnh v\u00e0 th\u01b0\u1eddng nhanh h\u01a1n merge sort \u1edf b\u1ed9 nh\u1edb cache.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. So s\u00e1nh c\u00e1c thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Algorithm<\/th><th>Average Time<\/th><th>Worst Time<\/th><th>Space<\/th><th>Stable?<\/th><\/tr><\/thead><tbody><tr><td>Bubble Sort<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>Yes (simple)<\/td><\/tr><tr><td>Selection Sort<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>No<\/td><\/tr><tr><td>Insertion Sort<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>Yes<\/td><\/tr><tr><td>Quick Sort<\/td><td>O(n log n)<\/td><td>O(n\u00b2)<\/td><td>O(n) (naive) \/ O(log n) (in-place)<\/td><td>No (typical)<\/td><\/tr><tr><td>Python Timsort (built-in)<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><td>O(n)<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Khi n\u00e0o d\u00f9ng:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng h\u00e0m d\u1ef1ng s\u1eb5n cho h\u1ea7u h\u1ebft tr\u01b0\u1eddng h\u1ee3p (hi\u1ec7u su\u1ea5t v\u00e0 \u1ed5n \u0111\u1ecbnh t\u1ed1t).<\/li>\n\n\n\n<li>D\u00f9ng Insertion cho m\u1ea3ng nh\u1ecf ho\u1eb7c g\u1ea7n s\u1eafp x\u1ebfp.<\/li>\n\n\n\n<li>D\u00f9ng Quick khi c\u1ea7n thu\u1eadt to\u00e1n nhanh trung b\u00ecnh, tri\u1ec3n khai attention \u0111\u1ebfn pivot.<\/li>\n\n\n\n<li>Bubble \/ Selection ch\u1ee7 y\u1ebfu cho m\u1ee5c \u0111\u00edch h\u1ecdc.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. T\u00f9y bi\u1ebfn vi\u1ec7c s\u1eafp x\u1ebfp<\/h2>\n\n\n\n<p>Trong th\u1ef1c t\u1ebf ta hay c\u1ea7n s\u1eafp x\u1ebfp theo ti\u00eau ch\u00ed ri\u00eang (v\u00ed d\u1ee5: s\u1eafp theo \u0111i\u1ec3m, \u01b0u ti\u00ean \u0111i\u1ec3m cao, ho\u1eb7c s\u1eafp strings theo \u0111\u1ed9 d\u00e0i).<\/p>\n\n\n\n<p>V\u00ed d\u1ee5: s\u1eafp sinh vi\u00ean theo \u0111i\u1ec3m gi\u1ea3m d\u1ea7n, n\u1ebfu \u0111i\u1ec3m b\u1eb1ng nhau th\u00ec theo t\u00ean t\u0103ng d\u1ea7n.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>students = &#91;\n    {\"name\": \"Alice\", \"score\": 85},\n    {\"name\": \"Bob\", \"score\": 92},\n    {\"name\": \"Charlie\", \"score\": 85}\n&#93;\n\n# Sort by score descending, then name ascending\nsorted_students = sorted(\n    students,\n    key=lambda s: (-s&#91;\"score\"&#93;, s&#91;\"name\"&#93;)\n)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">students <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">name<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Alice<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">score<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">85<\/span><span style=\"color: #ECEFF4\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">name<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Bob<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">score<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">92<\/span><span style=\"color: #ECEFF4\">},<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">name<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Charlie<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">score<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">85<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sort by score descending, then name ascending<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_students <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    students<\/span><span style=\"color: #ECEFF4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">key<\/span><span style=\"color: #81A1C1\">=lambda<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">s<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\">s<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">score<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> s<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">name<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">&#93;)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Gi\u1ea3i th\u00edch:<\/strong> <code>key<\/code> tr\u1ea3 v\u1ec1 m\u1ed9t tuple <code>(-score, name)<\/code>. Vi\u1ec7c d\u00f9ng <code>-score<\/code> \u0111\u1ea3o chi\u1ec1u \u0111\u1ec3 s\u1eafp gi\u1ea3m d\u1ea7n theo \u0111i\u1ec3m; th\u1ee9 t\u1ef1 ti\u1ebfp theo l\u00e0 <code>name<\/code> t\u0103ng d\u1ea7n.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Nh\u1eefng sai l\u1ea7m th\u01b0\u1eddng g\u1eb7p khi s\u1eafp x\u1ebfp Array<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u1ea7m l\u1eabn <code>sorted()<\/code> v\u00e0 <code>list.sort()<\/code> d\u1eabn \u0111\u1ebfn vi\u1ec7c v\u00f4 t\u00ecnh s\u1eeda m\u1ea3ng g\u1ed1c.<\/li>\n\n\n\n<li>Qu\u00ean l\u00e0 <code>sort()<\/code> tr\u1ea3 v\u1ec1 <code>None<\/code> (v\u00ec thao t\u00e1c in-place) \u2014 n\u00ean kh\u00f4ng g\u00e1n <code>x = list.sort()<\/code>.<\/li>\n\n\n\n<li>Qu\u00ean copy m\u1ea3ng khi c\u1ea7n gi\u1eef d\u1eef li\u1ec7u g\u1ed1c: <code>new = sorted(old)<\/code> thay v\u00ec <code>new = old<\/code> (ch\u1ec9 g\u00e1n tham chi\u1ebfu).<\/li>\n\n\n\n<li>Kh\u00f4ng x\u00e9t \u0111\u1ebfn \u1ed5n \u0111\u1ecbnh khi s\u1eafp x\u1ebfp d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p (c\u1ea7n stable \u0111\u1ec3 b\u1ea3o to\u00e0n ti\u00eau ch\u00ed ph\u1ee5).<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng thu\u1eadt to\u00e1n O(n\u00b2) cho dataset l\u1edbn d\u1eabn \u0111\u1ebfn th\u1eddi gian r\u1ea5t ch\u1eadm.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a Sorting trong l\u1eadp tr\u00ecnh<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Binary Search:<\/strong> ch\u1ec9 \u00e1p d\u1ee5ng khi d\u1eef li\u1ec7u \u0111\u00e3 s\u1eafp x\u1ebfp.<\/li>\n\n\n\n<li><strong>X\u1eed l\u00fd d\u1eef li\u1ec7u:<\/strong> th\u1ed1ng k\u00ea, t\u00ednh ranking, top-k.<\/li>\n\n\n\n<li><strong>Chu\u1ed7i thao t\u00e1c trong thu\u1eadt to\u00e1n ph\u1ee9c t\u1ea1p:<\/strong> merge step (merge sort), sweep-line algorithms, two-pointer techniques.<\/li>\n\n\n\n<li><strong>Giao di\u1ec7n v\u00e0 UX:<\/strong> s\u1eafp x\u1ebfp danh s\u00e1ch theo t\u00ean\/ng\u00e0y\/\u0111i\u1ec3m gi\u00fap ng\u01b0\u1eddi d\u00f9ng nhanh t\u00ecm th\u00f4ng tin.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">8. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p>S\u1eafp x\u1ebfp Array l\u00e0 k\u1ef9 n\u0103ng c\u01a1 b\u1ea3n nh\u01b0ng c\u1ef1c k\u1ef3 quan tr\u1ecdng trong l\u1eadp tr\u00ecnh. M\u00ecnh khuy\u1ebfn kh\u00edch:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng h\u00e0m d\u1ef1ng s\u1eb5n (<code>sorted<\/code>, <code>list.sort()<\/code>) trong ph\u1ea7n l\u1edbn tr\u01b0\u1eddng h\u1ee3p \u0111\u1ec3 \u0111\u1ea1t hi\u1ec7u n\u0103ng v\u00e0 \u0111\u1ed9 \u1ed5n \u0111\u1ecbnh t\u1ed1t.<\/li>\n\n\n\n<li>T\u1ef1 tri\u1ec3n khai v\u00e0 hi\u1ec3u c\u00e1c thu\u1eadt to\u00e1n: Bubble, Selection, Insertion, Quick \u0111\u1ec3 n\u1eafm r\u00f5 b\u1ea3n ch\u1ea5t time\/space trade-off.<\/li>\n\n\n\n<li>Bi\u1ebft khi n\u00e0o c\u1ea7n t\u1ed1i \u01b0u: n\u1ebfu dataset l\u1edbn, ch\u1ecdn thu\u1eadt to\u00e1n O(n log n) \u1ed5n \u0111\u1ecbnh v\u00e0 ch\u00fa \u00fd b\u1ed9 nh\u1edb; n\u1ebfu m\u1ea3ng g\u1ea7n s\u1eafp x\u1ebfp, Insertion r\u1ea5t hi\u1ec7u qu\u1ea3.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">9. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Cormen, T. H., Leiserson, C. E., Rivest, R. L., &amp; Stein, C. (2009). <em>Introduction to Algorithms<\/em> (3rd ed.). MIT Press.<\/li>\n\n\n\n<li>Python Software Foundation. (2025). <em>Sorting HOW TO<\/em>. Python 3 Documentation. Retrieved from <a>https:\/\/docs.python.org\/3\/howto\/sorting.html<\/a><\/li>\n\n\n\n<li>GeeksforGeeks. (2025). <em>Sorting Algorithms<\/em>. Retrieved from <a>https:\/\/www.geeksforgeeks.org\/sorting-algorithms\/<\/a><\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>B\u00e0i vi\u1ebft n\u00e0y m\u00ecnh s\u1ebd gi\u1ea3i th\u00edch chi ti\u1ebft kh\u00e1i ni\u1ec7m s\u1eafp x\u1ebfp Array, c\u00e1ch d\u00f9ng h\u00e0m d\u1ef1ng s\u1eb5n trong Python, \u0111\u1ed3ng th\u1eddi tri\u1ec3n khai v\u00e0 ph\u00e2n t\u00edch c\u00e1c thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u1ed5 bi\u1ebfn (Bubble, Selection, Insertion, Quick).<\/p>\n","protected":false},"author":1,"featured_media":1698,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1502","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1502"}],"version-history":[{"count":2,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1502\/revisions"}],"predecessor-version":[{"id":2443,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1502\/revisions\/2443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/1698"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}