This commit is contained in:
krahets 2024-01-23 19:19:57 +08:00
parent bd5e972bc9
commit 43afdd3137
3 changed files with 167 additions and 129 deletions

View file

@ -612,7 +612,7 @@ comments: true
## 11.5.4   基准数优化
**快速排序在某些输入下的时间效率可能降低**。举一个极端例子,假设输入数组是完全倒序的,由于我们选择最左端元素作为基准数,那么在哨兵划分完成后,基准数被交换至数组最右端,导致左子数组长度为 $n - 1$、右子数组长度为 $0$ 。如此递归下去,每轮哨兵划分后的右子数组长度都为 $0$ ,分治策略失效,快速排序退化为“冒泡排序”。
**快速排序在某些输入下的时间效率可能降低**。举一个极端例子,假设输入数组是完全倒序的,由于我们选择最左端元素作为基准数,那么在哨兵划分完成后,基准数被交换至数组最右端,导致左子数组长度为 $n - 1$、右子数组长度为 $0$ 。如此递归下去,每轮哨兵划分后都有一个子数组的长度为 $0$ ,分治策略失效,快速排序退化为“冒泡排序”的近似形式
为了尽量避免这种情况发生,**我们可以优化哨兵划分中的基准数的选取策略**。例如,我们可以随机选取一个元素作为基准数。然而,如果运气不佳,每次都选到不理想的基准数,效率仍然不尽如人意。

View file

@ -14,41 +14,109 @@ hide:
<!-- background -->
<img src="assets/hero/universe_bg.png" class="hero-bg">
<!-- hero image -->
<div class="hero-image-div">
<!-- planets -->
<img src="assets/hero/ground.png"
style="position: absolute; width: auto; height: 26.445%; left: 28.211%; top: 54.145%;">
<img src="assets/hero/links.png"
style="position: absolute; width: auto; height: 78.751%; left: 10.545%; top: 7.326%;">
<a href="/chapter_introduction/">
<img class="hero-on-hover" src="assets/hero/astronaut.png" style="height: 46.673%; left: 35.413%; top: 24.343%;">
<p class="hero-caption" style="left: 52.244%; top: 20.919%;">初识算法</p>
</a>
<a href="/chapter_computational_complexity/">
<img class="hero-on-hover" src="assets/hero/chapter_computational_complexity.png"
style="height: 12.347%; left: 36.267%; top: 37.653%;">
<p class="hero-caption" style="left: 39.244%; top: 33.919%;">复杂度</p>
</a>
<a href="/chapter_array_and_linkedlist/">
<img class="hero-on-hover" src="assets/hero/chapter_array_and_linkedlist.png"
style="height: 22.242%; left: 73.242%; top: 52.481%;">
<p class="hero-caption" style="left: 90.897%; top: 76.259%;">数组与链表</p>
</a>
<a href="/chapter_stack_and_queue/">
<img class="hero-on-hover" src="assets/hero/chapter_stack_and_queue.png"
style="height: 14.302%; left: 62.646%; top: 77.875%;">
<p class="hero-caption" style="left: 78.371%; top: 90.25%;">栈与队列</p>
</a>
<a href="/chapter_hashing/">
<img class="hero-on-hover" src="assets/hero/chapter_hashing.png"
style="height: 15.266%; left: 63.281%; top: 27.933%;">
<p class="hero-caption" style="left: 68.862%; top: 46.292%;">哈希表</p>
</a>
<a href="/chapter_tree/">
<img class="hero-on-hover" src="assets/hero/chapter_tree.png"
style="height: 19.615%; left: 80.137%; top: 26.678%;">
<p class="hero-caption" style="left: 96.159%; top: 44.8%;"></p>
</a>
<a href="/chapter_heap/">
<img class="hero-on-hover" src="assets/hero/chapter_heap.png"
style="height: 10.566%; left: 77.226%; top: 11.559%;">
<p class="hero-caption" style="left: 88.103%; top: 15.422%;"></p>
</a>
<a href="/chapter_graph/">
<img class="hero-on-hover" src="assets/hero/chapter_graph.png"
style="height: 16.112%; left: 51.854%; top: 5.575%;">
<p class="hero-caption" style="left: 71.195%; top: 6.503%;"></p>
</a>
<a href="/chapter_searching/">
<img class="hero-on-hover" src="assets/hero/chapter_searching.png"
style="height: 15.149%; left: 18.185%; top: 16.404%;">
<p class="hero-caption" style="left: 14.556%; top: 20.876%;">搜索</p>
</a>
<a href="/chapter_sorting/">
<img class="hero-on-hover" src="assets/hero/chapter_sorting.png"
style="height: 9.574%; left: 25.409%; top: 40.747%;">
<p class="hero-caption" style="left: 28.805%; top: 53.808%;">排序</p>
</a>
<a href="/chapter_divide_and_conquer/">
<img class="hero-on-hover" src="assets/hero/chapter_divide_and_conquer.png"
style="height: 18.681%; left: 32.721%; top: 4.816%;">
<p class="hero-caption" style="left: 31.42%; top: 8.679%;">分治</p>
</a>
<a href="/chapter_backtracking/">
<img class="hero-on-hover" src="assets/hero/chapter_backtracking.png"
style="height: 17.338%; left: 4.875%; top: 32.925%;">
<p class="hero-caption" style="left: 4.742%; top: 50.113%;">回溯</p>
</a>
<a href="/chapter_dynamic_programming/">
<img class="hero-on-hover" src="assets/hero/chapter_dynamic_programming.png"
style="height: 15.47%; left: 9.406%; top: 57.472%;">
<p class="hero-caption" style="left: 8.561%; top: 75.351%;">动态规划</p>
</a>
<a href="/chapter_greedy/">
<img class="hero-on-hover" src="assets/hero/chapter_greedy.png"
style="height: 14.127%; left: 23.132%; top: 75.803%;">
<p class="hero-caption" style="left: 21.619%; top: 86.85%;">贪心</p>
</a>
</div>
<!-- heading and buttons -->
<div style="width: 100%; position: absolute; transform: translateX(-50%); left: 50%; bottom: min(2vh, 3vw);">
<img style="height: min(9vh, 12vw);"
src="https://readme-typing-svg.demolab.com/?font=Noto+Sans+SC&weight=400&duration=3500&pause=2000&color=FFF&center=true&vCenter=true&random=false&width=200&lines=Hello%2C+%E7%AE%97%E6%B3%95+!"
alt="hello-algo-typing-svg" />
<p style="color: #fff; margin-top: max(-0.6vh, -1.2vw); margin-bottom: min(2.5vh, 4vw);">
<p style="color: #fff; margin-top: max(-1vh, -2vw); margin-bottom: min(2vh, 3.5vw);">
动画图解、一键运行的数据结构与算法教程
</p>
<div>
<a href="https://www.hello-algo.com/chapter_preface/" class="rounded-button hero-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="15.5px"
viewBox="0 0 576 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.-->
<a href="/chapter_preface/" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
<path
d="M249.6 471.5c10.8 3.8 22.4-4.1 22.4-15.5V78.6c0-4.2-1.6-8.4-5-11C247.4 52 202.4 32 144 32C93.5 32 46.3 45.3 18.1 56.1C6.8 60.5 0 71.7 0 83.8V454.1c0 11.9 12.8 20.2 24.1 16.5C55.6 460.1 105.5 448 144 448c33.9 0 79 14 105.6 23.5zm76.8 0C353 462 398.1 448 432 448c38.5 0 88.4 12.1 119.9 22.6c11.3 3.8 24.1-4.6 24.1-16.5V83.8c0-12.1-6.8-23.3-18.1-27.6C529.7 45.3 482.5 32 432 32c-58.4 0-103.4 20-123 35.6c-3.3 2.6-5 6.8-5 11V456c0 11.4 11.7 19.3 22.4 15.5z">
</path>
d="M160 96a96 96 0 1 1 192 0A96 96 0 1 1 160 96zm80 152V512l-48.4-24.2c-20.9-10.4-43.5-17-66.8-19.3l-96-9.6C12.5 457.2 0 443.5 0 427V224c0-17.7 14.3-32 32-32H62.3c63.6 0 125.6 19.6 177.7 56zm32 264V248c52.1-36.4 114.1-56 177.7-56H480c17.7 0 32 14.3 32 32V427c0 16.4-12.5 30.2-28.8 31.8l-96 9.6c-23.2 2.3-45.9 8.9-66.8 19.3L272 512z" />
</svg>
开始阅读
<p>开始阅读</p>
</a>
<a href="https://github.com/krahets/hello-algo" class="rounded-button hero-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="15.5px"
<a href="https://github.com/krahets/hello-algo" class="rounded-button">
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 496 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.-->
<path
d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3 .3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5 .3-6.2 2.3zm44.2-1.7c-2.9 .7-4.9 2.6-4.6 4.9 .3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3 .7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3 .3 2.9 2.3 3.9 1.6 1 3.6 .7 4.3-.7 .7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3 .7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3 .7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z">
</path>
</svg>
代码仓库
</a>
<a href="https://github.com/krahets/hello-algo/releases" class="rounded-button hero-btn">
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="15.5px"
viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2023 Fonticons, Inc.-->
<path
d="M0 64C0 28.7 28.7 0 64 0L224 0l0 128c0 17.7 14.3 32 32 32l128 0 0 144-208 0c-35.3 0-64 28.7-64 64l0 144-48 0c-35.3 0-64-28.7-64-64L0 64zm384 64l-128 0L256 0 384 128zM176 352l32 0c30.9 0 56 25.1 56 56s-25.1 56-56 56l-16 0 0 32c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-48 0-80c0-8.8 7.2-16 16-16zm32 80c13.3 0 24-10.7 24-24s-10.7-24-24-24l-16 0 0 48 16 0zm96-80l32 0c26.5 0 48 21.5 48 48l0 64c0 26.5-21.5 48-48 48l-32 0c-8.8 0-16-7.2-16-16l0-128c0-8.8 7.2-16 16-16zm32 128c8.8 0 16-7.2 16-16l0-64c0-8.8-7.2-16-16-16l-16 0 0 96 16 0zm80-112c0-8.8 7.2-16 16-16l48 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 32 32 0c8.8 0 16 7.2 16 16s-7.2 16-16 16l-32 0 0 48c0 8.8-7.2 16-16 16s-16-7.2-16-16l0-64 0-64z">
</path>
</svg>
下载 PDF
<p>代码仓库</p>
</a>
</div>
<!-- arrow -->
@ -60,84 +128,6 @@ hide:
</svg>
</div>
</div>
<!-- hero image -->
<div class="hero-image-div">
<!-- planets -->
<img src="assets/hero/ground.png"
style="position: absolute; width: auto; height: 26.445%; left: 28.211%; top: 54.145%;">
<img src="assets/hero/links.png"
style="position: absolute; width: auto; height: 78.751%; left: 10.545%; top: 7.326%;">
<a href="https://www.hello-algo.com/chapter_introduction/">
<img class="hero-on-hover" src="assets/hero/astronaut.png" style="height: 46.673%; left: 35.413%; top: 24.343%;">
<p class="hero-caption" style="left: 52.244%; top: 20.919%;">初识算法</p>
</a>
<a href="https://www.hello-algo.com/chapter_computational_complexity/">
<img class="hero-on-hover" src="assets/hero/chapter_computational_complexity.png"
style="height: 12.347%; left: 36.267%; top: 37.653%;">
<p class="hero-caption" style="left: 39.244%; top: 33.919%;">复杂度</p>
</a>
<a href="https://www.hello-algo.com/chapter_array_and_linkedlist/">
<img class="hero-on-hover" src="assets/hero/chapter_array_and_linkedlist.png"
style="height: 22.242%; left: 73.242%; top: 52.481%;">
<p class="hero-caption" style="left: 90.897%; top: 76.259%;">数组与链表</p>
</a>
<a href="https://www.hello-algo.com/chapter_stack_and_queue/">
<img class="hero-on-hover" src="assets/hero/chapter_stack_and_queue.png"
style="height: 14.302%; left: 62.646%; top: 77.875%;">
<p class="hero-caption" style="left: 78.371%; top: 90.25%;">栈与队列</p>
</a>
<a href="https://www.hello-algo.com/chapter_hashing/">
<img class="hero-on-hover" src="assets/hero/chapter_hashing.png"
style="height: 15.266%; left: 63.281%; top: 27.933%;">
<p class="hero-caption" style="left: 68.862%; top: 46.292%;">哈希表</p>
</a>
<a href="https://www.hello-algo.com/chapter_tree/">
<img class="hero-on-hover" src="assets/hero/chapter_tree.png"
style="height: 19.615%; left: 80.137%; top: 26.678%;">
<p class="hero-caption" style="left: 96.159%; top: 44.8%;"></p>
</a>
<a href="https://www.hello-algo.com/chapter_heap/">
<img class="hero-on-hover" src="assets/hero/chapter_heap.png"
style="height: 10.566%; left: 77.226%; top: 11.559%;">
<p class="hero-caption" style="left: 88.103%; top: 15.422%;"></p>
</a>
<a href="https://www.hello-algo.com/chapter_graph/">
<img class="hero-on-hover" src="assets/hero/chapter_graph.png"
style="height: 16.112%; left: 51.854%; top: 5.575%;">
<p class="hero-caption" style="left: 71.195%; top: 6.503%;"></p>
</a>
<a href="https://www.hello-algo.com/chapter_searching/">
<img class="hero-on-hover" src="assets/hero/chapter_searching.png"
style="height: 15.149%; left: 18.185%; top: 16.404%;">
<p class="hero-caption" style="left: 14.556%; top: 20.876%;">搜索</p>
</a>
<a href="https://www.hello-algo.com/chapter_sorting/">
<img class="hero-on-hover" src="assets/hero/chapter_sorting.png"
style="height: 9.574%; left: 25.409%; top: 40.747%;">
<p class="hero-caption" style="left: 28.805%; top: 53.808%;">排序</p>
</a>
<a href="https://www.hello-algo.com/chapter_divide_and_conquer/">
<img class="hero-on-hover" src="assets/hero/chapter_divide_and_conquer.png"
style="height: 18.681%; left: 32.721%; top: 4.816%;">
<p class="hero-caption" style="left: 31.42%; top: 8.679%;">分治</p>
</a>
<a href="https://www.hello-algo.com/chapter_backtracking/">
<img class="hero-on-hover" src="assets/hero/chapter_backtracking.png"
style="height: 17.338%; left: 4.875%; top: 32.925%;">
<p class="hero-caption" style="left: 4.742%; top: 50.113%;">回溯</p>
</a>
<a href="https://www.hello-algo.com/chapter_dynamic_programming/">
<img class="hero-on-hover" src="assets/hero/chapter_dynamic_programming.png"
style="height: 15.47%; left: 9.406%; top: 57.472%;">
<p class="hero-caption" style="left: 8.561%; top: 75.351%;">动态规划</p>
</a>
<a href="https://www.hello-algo.com/chapter_greedy/">
<img class="hero-on-hover" src="assets/hero/chapter_greedy.png"
style="height: 14.127%; left: 23.132%; top: 75.803%;">
<p class="hero-caption" style="left: 21.619%; top: 86.85%;">贪心</p>
</a>
</div>
</section>
<!-- Section: brief introduction -->
@ -177,6 +167,18 @@ hide:
<img class="device-on-hover" src="assets/hero/web_iphone.png" style="height: 57.27%; left: 2%; bottom: 0%;">
</div>
<p style="margin-top: 2em;">提供网页版和 PDF 版,兼容 PC、平板和手机随时随地阅读</p>
<div class="text-button-container">
<a href="https://github.com/krahets/hello-algo/releases">
<div class="text-button">
<p>下载 PDF</p>
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 320 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.-->
<path
d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
</svg>
</div>
</a>
</div>
</div>
</section>

View file

@ -112,6 +112,11 @@
text-transform: none;
}
.md-typeset a:hover {
color: var(--md-typeset-a-color);
text-decoration: underline;
}
.md-typeset code {
border-radius: 0.2rem;
}
@ -150,34 +155,6 @@ body {
width: 100%; /* Default to full width */
}
/* rounded button */
.rounded-button {
display: inline-flex; /* Use flexbox for alignment */
align-items: center; /* Align items vertically */
justify-content: center; /* Center items horizontally */
border-radius: 100px; /* Circular corners */
padding: 9px 18px; /* Padding around the text */
margin: 0.15em 0;
border: none; /* Removes default border */
background-color: var(--md-typeset-btn-color); /* Background color */
color: var(--md-primary-fg-color) !important; /* Text color */
font-size: 0.85em; /* Font size */
text-align: center; /* Center the text */
text-decoration: none; /* Remove underline from anchor text */
cursor: pointer; /* Pointer cursor on hover */
}
.rounded-button:hover {
background-color: var(--md-typeset-btn-hover-color);
}
.rounded-button svg {
fill: var(--md-primary-fg-color); /* Fill SVG icon with text color */
width: 1.2em;
height: auto;
margin-right: 0.5em; /* Add some space between the SVG icon and the text */
}
/* Admonition for python tutor */
.md-typeset .admonition.pythontutor,
.md-typeset details.pythontutor {
@ -237,10 +214,69 @@ body {
max-width: 70vw;
}
.hero-btn {
/* rounded button */
.rounded-button {
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 10em;
margin: 0 0.1em;
padding: 0.5em 1.3em;
font-size: 0.85em;
border: none;
background-color: var(--md-typeset-btn-color);
color: var(--md-primary-fg-color) !important;
text-align: center;
text-decoration: none;
cursor: pointer;
}
.rounded-button:hover {
background-color: var(--md-typeset-btn-hover-color);
}
.rounded-button p {
margin: 0;
margin-bottom: 0.1em;
}
.rounded-button svg {
fill: var(--md-primary-fg-color);
width: auto;
height: 1.2em;
margin-right: 0.5em;
}
/* text button */
.text-button-container {
width: 100%;
display: flex;
flex-direction: row;
justify-content: center;
height: auto;
margin: 0 auto;
}
.text-button {
display: flex;
flex-direction: row;
align-items: center;
color: var(--md-typeset-btn-color);
text-decoration: none;
margin: 0 1em;
}
.text-button:hover {
text-decoration: underline;
}
.text-button svg {
display: inline-block;
fill: var(--md-typeset-btn-color);
width: auto;
height: 0.9em;
background-size: cover;
padding-top: 0.17em;
margin-left: 0.4em;
}
/* hero image */
@ -284,8 +320,8 @@ a:hover .hero-on-hover {
}
a:hover .hero-caption {
text-decoration: underline;
color: var(--md-typeset-btn-color);
font-weight: bold;
}
/* code badge */