{"id":1516,"date":"2025-09-28T16:39:01","date_gmt":"2025-09-28T09:39:01","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1516"},"modified":"2025-11-05T23:39:22","modified_gmt":"2025-11-05T16:39:22","slug":"sap-xep-cac-phan-tu-trong-list-voi-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/sap-xep-cac-phan-tu-trong-list-voi-python\/","title":{"rendered":"S\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed trong List v\u1edbi Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Trong l\u1eadp tr\u00ecnh Python, <strong>List<\/strong> l\u00e0 m\u1ed9t trong nh\u1eefng ki\u1ec3u d\u1eef li\u1ec7u linh ho\u1ea1t v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u nh\u1ea5t. M\u1ed9t List c\u00f3 th\u1ec3 ch\u1ee9a s\u1ed1, chu\u1ed7i, \u0111\u1ed1i t\u01b0\u1ee3ng\u2026 v\u00e0 th\u01b0\u1eddng xuy\u00ean ch\u00fang ta c\u1ea7n <strong>s\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed trong List<\/strong> \u0111\u1ec3 ti\u1ec7n x\u1eed l\u00fd ho\u1eb7c hi\u1ec3n th\u1ecb.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5: b\u1ea1n c\u00f3 m\u1ed9t danh s\u00e1ch \u0111i\u1ec3m thi c\u1ee7a sinh vi\u00ean, b\u1ea1n c\u1ea7n s\u1eafp theo \u0111i\u1ec3m cao \u2192 th\u1ea5p; ho\u1eb7c m\u1ed9t danh s\u00e1ch t\u00ean, c\u1ea7n s\u1eafp theo th\u1ee9 t\u1ef1 alphabet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong b\u00e0i n\u00e0y, m\u00ecnh s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u chi ti\u1ebft c\u00e1ch s\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed trong List b\u1eb1ng:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>H\u00e0m d\u1ef1ng s\u1eb5n<\/strong> trong Python (<code>sort()<\/code>, <code>sorted()<\/code>).<\/li>\n\n\n\n<li><strong>T\u1ef1 tri\u1ec3n khai thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp<\/strong> \u0111\u1ec3 hi\u1ec3u b\u1ea3n ch\u1ea5t.<\/li>\n\n\n\n<li><strong>So s\u00e1nh c\u00e1c thu\u1eadt to\u00e1n<\/strong>, n\u00eau ra t\u00ecnh hu\u1ed1ng \u00e1p d\u1ee5ng.<\/li>\n\n\n\n<li><strong>Sai l\u1ea7m th\u01b0\u1eddng g\u1eb7p v\u00e0 \u1ee9ng d\u1ee5ng th\u1ef1c t\u1ebf<\/strong> khi l\u00e0m vi\u1ec7c v\u1edbi List.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. T\u1ed5ng quan v\u1ec1 List v\u00e0 Sorting<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>List trong Python<\/strong>: l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u \u0111\u1ed9ng, c\u00f3 th\u1ec3 ch\u1ee9a nhi\u1ec1u ki\u1ec3u ph\u1ea7n t\u1eed, thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc linh ho\u1ea1t, h\u1ed7 tr\u1ee3 thao t\u00e1c th\u00eam\/x\u00f3a\/s\u1eeda d\u1ec5 d\u00e0ng.<\/li>\n\n\n\n<li><strong>Sorting<\/strong> (s\u1eafp x\u1ebfp): l\u00e0 vi\u1ec7c \u0111\u01b0a c\u00e1c ph\u1ea7n t\u1eed trong List v\u1ec1 m\u1ed9t th\u1ee9 t\u1ef1 x\u00e1c \u0111\u1ecbnh.\n<ul class=\"wp-block-list\">\n<li>Th\u01b0\u1eddng g\u1eb7p: t\u0103ng d\u1ea7n (ascending), gi\u1ea3m d\u1ea7n (descending).<\/li>\n\n\n\n<li>Ngo\u00e0i ra c\u00f3 th\u1ec3 t\u00f9y ch\u1ec9nh (v\u00ed d\u1ee5 s\u1eafp chu\u1ed7i theo \u0111\u1ed9 d\u00e0i).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">L\u1ee3i \u00edch c\u1ee7a vi\u1ec7c s\u1eafp x\u1ebfp List:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Gi\u00fap <strong>t\u00ecm ki\u1ebfm<\/strong> nhanh h\u01a1n (binary search).<\/li>\n\n\n\n<li>D\u1eef li\u1ec7u tr\u1edf n\u00ean <strong>d\u1ec5 \u0111\u1ecdc<\/strong> v\u00e0 d\u1ec5 ph\u00e2n t\u00edch.<\/li>\n\n\n\n<li>L\u00e0 b\u01b0\u1edbc <strong>trung gian quan tr\u1ecdng<\/strong> trong nhi\u1ec1u thu\u1eadt to\u00e1n kh\u00e1c.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. S\u1eafp x\u1ebfp List b\u1eb1ng h\u00e0m d\u1ef1ng s\u1eb5n trong Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python cung c\u1ea5p s\u1eb5n hai c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>list.sort()<\/code> \u2014 s\u1eafp x\u1ebfp <strong>in-place<\/strong>, thay \u0111\u1ed5i List g\u1ed1c.<\/li>\n\n\n\n<li><code>sorted(list)<\/code> \u2014 tr\u1ea3 v\u1ec1 <strong>List m\u1edbi<\/strong> \u0111\u00e3 s\u1eafp x\u1ebfp, List g\u1ed1c kh\u00f4ng \u0111\u1ed5i.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">V\u00ed d\u1ee5 minh h\u1ecda<\/h3>\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;7, 2, 9, 4&#93;\n\n# Sort in ascending order (default)\nnumbers.sort()\nprint(numbers)  # &#91;2, 4, 7, 9&#93;\n\n# Sort in descending order\nnumbers.sort(reverse=True)\nprint(numbers)  # &#91;9, 7, 4, 2&#93;\n\n# Using sorted() to keep original list\noriginal = &#91;3, 1, 2&#93;\nnew_sorted = sorted(original)\nprint(original)   # &#91;3, 1, 2&#93;\nprint(new_sorted) # &#91;1, 2, 3&#93;\n\n# Custom sorting: sort strings by length\nwords = &#91;\"apple\", \"kiwi\", \"banana\"&#93;\nwords.sort(key=len)\nprint(words)  # &#91;'kiwi', 'apple', 'banana'&#93;\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\">7<\/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\">4<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sort in ascending order (default)<\/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>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/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\"># &#91;2, 4, 7, 9&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sort in descending order<\/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: #D8DEE9\">reverse<\/span><span style=\"color: #81A1C1\">=True<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/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\"># &#91;9, 7, 4, 2&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using sorted() to keep original list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">original <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">3<\/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\">2<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">new_sorted <\/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\">original<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">original<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># &#91;3, 1, 2&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">new_sorted<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #616E88\"># &#91;1, 2, 3&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Custom sorting: 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\">kiwi<\/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>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">words<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;&#39;kiwi&#39;, &#39;apple&#39;, &#39;banana&#39;&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Gi\u1ea3i th\u00edch<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>list.sort()<\/strong><\/code> th\u01b0\u1eddng nhanh h\u01a1n m\u1ed9t ch\u00fat v\u00ec kh\u00f4ng t\u1ea1o ra List m\u1edbi.<\/li>\n\n\n\n<li><code>sorted()<\/code> h\u1eefu \u00edch khi b\u1ea1n mu\u1ed1n gi\u1eef nguy\u00ean d\u1eef li\u1ec7u ban \u0111\u1ea7u.<\/li>\n\n\n\n<li>C\u1ea3 hai \u0111\u1ec1u s\u1eed d\u1ee5ng <strong>Timsort<\/strong> (trung b\u00ecnh O(n log n), stable).<\/li>\n\n\n\n<li><code>key=<\/code> cho ph\u00e9p b\u1ea1n \u0111\u1ecbnh ngh\u0129a c\u00e1ch so s\u00e1nh (v\u00ed d\u1ee5 theo \u0111\u1ed9 d\u00e0i chu\u1ed7i, theo \u0111i\u1ec3m s\u1ed1).<\/li>\n\n\n\n<li><code>reverse=True<\/code> \u0111\u1ec3 s\u1eafp x\u1ebfp ng\u01b0\u1ee3c.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. C\u00e1c thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u1ed5 bi\u1ebfn cho List<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">D\u00f9 c\u00f3 s\u1eb5n <code>sort()<\/code> v\u00e0 <code>sorted()<\/code>, nh\u01b0ng vi\u1ec7c hi\u1ec3u thu\u1eadt to\u00e1n c\u01a1 b\u1ea3n gi\u00fap b\u1ea1n n\u1eafm r\u00f5 b\u1ea3n ch\u1ea5t, c\u00f3 th\u1ec3 t\u1ed1i \u01b0u trong tr\u01b0\u1eddng h\u1ee3p \u0111\u1eb7c th\u00f9.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Bubble Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00dd t\u01b0\u1edfng:<\/strong> So s\u00e1nh t\u1eebng c\u1eb7p k\u1ec1 nhau, \u0111\u1ed5i ch\u1ed7 n\u1ebfu sai th\u1ee9 t\u1ef1, l\u1eb7p l\u1ea1i \u0111\u1ebfn khi m\u1ea3ng \u0111\u01b0\u1ee3c s\u1eafp.<\/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(lst):\n    n = len(lst)\n    for i in range(n):\n        swapped = False\n        for j in range(0, n - i - 1):\n            if lst&#91;j&#93; > lst&#91;j + 1&#93;:\n                # Swap elements\n                lst&#91;j&#93;, lst&#91;j + 1&#93; = lst&#91;j + 1&#93;, lst&#91;j&#93;\n                swapped = True\n        if not swapped:\n            break\n    return lst\n\nprint(bubble_sort(&#91;5, 3, 8, 4&#93;))\n# Output: &#91;3, 4, 5, 8&#93;\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\">lst<\/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\">lst<\/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: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> lst<\/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\"> lst<\/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\">                <\/span><span style=\"color: #616E88\"># Swap elements<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">                lst<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">j<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> lst<\/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\"> lst<\/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\"> lst<\/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: #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\"> lst<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">bubble_sort<\/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\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">8<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">&#93;))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Output: &#91;3, 4, 5, 8&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0110\u1ed9 ph\u1ee9c t\u1ea1p:<\/strong> O(n\u00b2). Th\u00edch h\u1ee3p cho h\u1ecdc thu\u1eadt, kh\u00f4ng ph\u00f9 h\u1ee3p dataset l\u1edbn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 Selection Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00dd t\u01b0\u1edfng:<\/strong> \u1ede m\u1ed7i v\u00f2ng l\u1eb7p, ch\u1ecdn ph\u1ea7n t\u1eed nh\u1ecf nh\u1ea5t trong List c\u00f2n l\u1ea1i v\u00e0 \u0111\u01b0a v\u1ec1 \u0111\u1ea7u.<\/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(lst):\n    n = len(lst)\n    for i in range(n):\n        min_index = i\n        for j in range(i + 1, n):\n            if lst&#91;j&#93; &lt; lst&#91;min_index&#93;:\n                min_index = j\n        lst&#91;i&#93;, lst&#91;min_index&#93; = lst&#91;min_index&#93;, lst&#91;i&#93;\n    return lst\n\nprint(selection_sort(&#91;64, 25, 12, 22, 11&#93;))\n# Output: &#91;11, 12, 22, 25, 64&#93;\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\">lst<\/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\">lst<\/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\"> lst<\/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\"> lst<\/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\">        lst<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">i<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> lst<\/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\"> lst<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">min_index<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> lst<\/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\"> lst<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">selection_sort<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">64<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">25<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">12<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">22<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">11<\/span><span style=\"color: #ECEFF4\">&#93;))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Output: &#91;11, 12, 22, 25, 64&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0110\u1ed9 ph\u1ee9c t\u1ea1p:<\/strong> O(n\u00b2). Kh\u00f4ng stable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 Insertion Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00dd t\u01b0\u1edfng:<\/strong> Duy\u1ec7t t\u1eeb tr\u00e1i sang ph\u1ea3i, ch\u00e8n ph\u1ea7n t\u1eed hi\u1ec7n t\u1ea1i v\u00e0o v\u1ecb tr\u00ed \u0111\u00fang trong \u0111o\u1ea1n \u0111\u00e3 s\u1eafp.<\/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(lst):\n    for i in range(1, len(lst)):\n        key = lst&#91;i&#93;\n        j = i - 1\n        while j >= 0 and lst&#91;j&#93; > key:\n            lst&#91;j + 1&#93; = lst&#91;j&#93;\n            j -= 1\n        lst&#91;j + 1&#93; = key\n    return lst\n\nprint(insertion_sort(&#91;12, 11, 13, 5, 6&#93;))\n# Output: &#91;5, 6, 11, 12, 13&#93;\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\">lst<\/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\">lst<\/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\"> lst<\/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: #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\"> lst<\/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\">            lst<\/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\"> lst<\/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\">        lst<\/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\"> lst<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">insertion_sort<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">12<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">11<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">13<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #ECEFF4\">&#93;))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Output: &#91;5, 6, 11, 12, 13&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0110\u1ed9 ph\u1ee9c t\u1ea1p:<\/strong> Trung b\u00ecnh O(n\u00b2), t\u1ed1t nh\u1ea5t O(n) (n\u1ebfu g\u1ea7n s\u1eafp x\u1ebfp). Stable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 Quick Sort<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00dd t\u01b0\u1edfng:<\/strong> Ch\u1ecdn m\u1ed9t pivot, chia List th\u00e0nh hai ph\u1ea7n (&lt; pivot v\u00e0 &gt; pivot), sau \u0111\u00f3 \u0111\u1ec7 quy.<\/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(lst):\n    if len(lst) &lt;= 1:\n        return lst\n    pivot = lst&#91;len(lst) \/\/ 2&#93;\n    left = &#91;x for x in lst if x &lt; pivot&#93;\n    middle = &#91;x for x in lst if x == pivot&#93;\n    right = &#91;x for x in lst if x > pivot&#93;\n    return quick_sort(left) + middle + quick_sort(right)\n\nprint(quick_sort(&#91;10, 7, 8, 9, 1, 5&#93;))\n# Output: &#91;1, 5, 7, 8, 9, 10&#93;\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\">lst<\/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\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lst<\/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\"> lst<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    pivot <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> lst<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">lst<\/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\"> lst <\/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\"> lst <\/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\"> lst <\/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: #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>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">quick_sort<\/span><span style=\"color: #ECEFF4\">(&#91;<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">8<\/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 style=\"color: #616E88\"># Output: &#91;1, 5, 7, 8, 9, 10&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0110\u1ed9 ph\u1ee9c t\u1ea1p:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trung b\u00ecnh: O(n log n).<\/li>\n\n\n\n<li>T\u1ec7 nh\u1ea5t: O(n\u00b2) (pivot kh\u00f4ng t\u1ed1t).<\/li>\n\n\n\n<li>Kh\u00f4ng stable.<\/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<\/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(log n)<\/td><td>No<\/td><\/tr><tr><td>Python Timsort<\/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<h2 class=\"wp-block-heading\">5. T\u00f9y bi\u1ebfn vi\u1ec7c s\u1eafp x\u1ebfp List<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">B\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng <code>key<\/code> \u0111\u1ec3 s\u1eafp x\u1ebfp List theo ti\u00eau ch\u00ed ri\u00eang.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">V\u00ed d\u1ee5: S\u1eafp x\u1ebfp danh s\u00e1ch 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<\/h3>\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\": 90},\n    {\"name\": \"Bob\", \"score\": 85},\n    {\"name\": \"Charlie\", \"score\": 90}\n&#93;\n\nsorted_students = sorted(\n    students,\n    key=lambda s: (-s&#91;\"score\"&#93;, s&#91;\"name\"&#93;)\n)\n\nprint(sorted_students)\n# Output: &#91;{'name': 'Alice', 'score': 90}, \n#          {'name': 'Charlie', 'score': 90}, \n#          {'name': 'Bob', 'score': 85}&#93;<\/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\">90<\/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\">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\">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\">90<\/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: #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>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_students<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Output: &#91;{&#39;name&#39;: &#39;Alice&#39;, &#39;score&#39;: 90}, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">#          {&#39;name&#39;: &#39;Charlie&#39;, &#39;score&#39;: 90}, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">#          {&#39;name&#39;: &#39;Bob&#39;, &#39;score&#39;: 85}&#93;<\/span><\/span><\/code><\/pre><\/div>\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 List<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Nh\u1ea7m <code>list.sort()<\/code> v\u1edbi <code>sorted()<\/code>.<\/li>\n\n\n\n<li>Qu\u00ean r\u1eb1ng <code>list.sort()<\/code> tr\u1ea3 v\u1ec1 <code>None<\/code>.<\/li>\n\n\n\n<li>G\u00e1n tham chi\u1ebfu thay v\u00ec copy khi c\u1ea7n gi\u1eef d\u1eef li\u1ec7u g\u1ed1c.<\/li>\n\n\n\n<li>Kh\u00f4ng c\u00e2n nh\u1eafc \u1ed5n \u0111\u1ecbnh (stable) khi d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p.<\/li>\n\n\n\n<li>D\u00f9ng thu\u1eadt to\u00e1n O(n\u00b2) cho List l\u1edbn g\u00e2y ch\u1eadm tr\u1ec5.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u1ee8ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a Sorting tr\u00ean List<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>T\u00ecm ki\u1ebfm nh\u1ecb ph\u00e2n:<\/strong> y\u00eau c\u1ea7u List \u0111\u00e3 s\u1eafp.<\/li>\n\n\n\n<li><strong>Ph\u00e2n t\u00edch d\u1eef li\u1ec7u:<\/strong> x\u1ebfp h\u1ea1ng, th\u1ed1ng k\u00ea.<\/li>\n\n\n\n<li><strong>Game &amp; AI:<\/strong> s\u1eafp th\u1ee9 t\u1ef1 \u0111\u1ed1i th\u1ee7, s\u1eafp s\u1ef1 ki\u1ec7n.<\/li>\n\n\n\n<li><strong>UX:<\/strong> hi\u1ec3n th\u1ecb d\u1eef li\u1ec7u theo ti\u00eau ch\u00ed mong mu\u1ed1n.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">8. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Qua b\u00e0i vi\u1ebft n\u00e0y, m\u00ecnh \u0111\u00e3 \u0111i t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>D\u00f9ng h\u00e0m d\u1ef1ng s\u1eb5n<\/strong> (<code>sort<\/code>, <code>sorted<\/code>) \u0111\u1ec3 s\u1eafp x\u1ebfp nhanh v\u00e0 hi\u1ec7u qu\u1ea3.<\/li>\n\n\n\n<li><strong>Hi\u1ec3u c\u00e1c thu\u1eadt to\u00e1n c\u01a1 b\u1ea3n<\/strong> nh\u01b0 Bubble, Selection, Insertion, Quick Sort.<\/li>\n\n\n\n<li><strong>Bi\u1ebft c\u00e1ch t\u00f9y bi\u1ebfn vi\u1ec7c s\u1eafp x\u1ebfp List<\/strong> v\u1edbi <code>key<\/code>.<\/li>\n\n\n\n<li><strong>Nh\u1eadn di\u1ec7n sai l\u1ea7m th\u01b0\u1eddng g\u1eb7p<\/strong> v\u00e0 c\u00e1ch tr\u00e1nh.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu b\u1ea1n l\u00e0m vi\u1ec7c v\u1edbi Python h\u00e0ng ng\u00e0y, h\u00e3y t\u1eadn d\u1ee5ng <code>sort()<\/code> v\u00e0 <code>sorted()<\/code> v\u00ec hi\u1ec7u su\u1ea5t cao v\u00e0 an to\u00e0n. Nh\u01b0ng c\u0169ng \u0111\u1eebng b\u1ecf qua vi\u1ec7c luy\u1ec7n t\u1eadp thu\u1eadt to\u00e1n th\u1ee7 c\u00f4ng \u2014 \u0111\u00e2y l\u00e0 c\u00e1ch \u0111\u1ec3 hi\u1ec3u c\u1eb7n k\u1ebd h\u01a1n v\u1ec1 c\u00e1ch m\u00e1y t\u00ednh l\u00e0m vi\u1ec7c.<\/p>\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>Trong b\u00e0i vi\u1ebft n\u00e0y, m\u00ecnh s\u1ebd ph\u00e2n t\u00edch chi ti\u1ebft c\u00e1c ph\u01b0\u01a1ng ph\u00e1p s\u1eafp x\u1ebfp List trong Python, bao g\u1ed3m c\u00e1ch d\u00f9ng h\u00e0m d\u1ef1ng s\u1eb5n v\u00e0 vi\u1ec7c t\u1ef1 c\u00e0i \u0111\u1eb7t thu\u1eadt to\u00e1n s\u1eafp x\u1ebfp ph\u1ed5 bi\u1ebfn nh\u01b0 Bubble Sort, Insertion Sort, Selection Sort, Quick Sort<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1516","post","type-post","status-publish","format-standard","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1516","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=1516"}],"version-history":[{"count":2,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1516\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1516\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}