📄 sort.html
字号:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>sort - perl pragma to control sort behaviour</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',1);</script>
<h1><a>sort - perl pragma to control sort behaviour</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#caveats">CAVEATS</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>sort - perl pragma to control <a href="../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> behaviour</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'stable'</span><span class="operator">;</span> <span class="comment"># guarantee stability</span>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'_quicksort'</span><span class="operator">;</span> <span class="comment"># use a quicksort algorithm</span>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'_mergesort'</span><span class="operator">;</span> <span class="comment"># use a mergesort algorithm</span>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'defaults'</span><span class="operator">;</span> <span class="comment"># revert to default behavior</span>
<span class="keyword">no</span> <span class="keyword">sort</span> <span class="string">'stable'</span><span class="operator">;</span> <span class="comment"># stability not important</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'_qsort'</span><span class="operator">;</span> <span class="comment"># alias for quicksort</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$current</span> <span class="operator">=</span> <span class="variable">sort::current</span><span class="operator">();</span> <span class="comment"># identify prevailing algorithm</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>With the <a href="../lib/Pod/perlfunc.html#item_sort"><code>sort</code></a> pragma you can control the behaviour of the builtin
<a href="../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> function.</p>
<p>In Perl versions 5.6 and earlier the quicksort algorithm was used to
implement <a href="../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a>, but in Perl 5.8 a mergesort algorithm was also made
available, mainly to guarantee worst case O(N log N) behaviour:
the worst case of quicksort is O(N**2). In Perl 5.8 and later,
quicksort defends against quadratic behaviour by shuffling large
arrays before sorting.</p>
<p>A stable sort means that for records that compare equal, the original
input ordering is preserved. Mergesort is stable, quicksort is not.
Stability will matter only if elements that compare equal can be
distinguished in some other way. That means that simple numerical
and lexical sorts do not profit from stability, since equal elements
are indistinguishable. However, with a comparison such as</p>
<pre>
<span class="operator">{</span> <span class="keyword">substr</span><span class="operator">(</span><span class="variable">$a</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">3</span><span class="operator">)</span> <span class="keyword">cmp</span> <span class="keyword">substr</span><span class="operator">(</span><span class="variable">$b</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">3</span><span class="operator">)</span> <span class="operator">}</span>
</pre>
<p>stability might matter because elements that compare equal on the
first 3 characters may be distinguished based on subsequent characters.
In Perl 5.8 and later, quicksort can be stabilized, but doing so will
add overhead, so it should only be done if it matters.</p>
<p>The best algorithm depends on many things. On average, mergesort
does fewer comparisons than quicksort, so it may be better when
complicated comparison routines are used. Mergesort also takes
advantage of pre-existing order, so it would be favored for using
<a href="../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> to merge several sorted arrays. On the other hand, quicksort
is often faster for small arrays, and on arrays of a few distinct
values, repeated many times. You can force the
choice of algorithm with this pragma, but this feels heavy-handed,
so the subpragmas beginning with a <code>_</code> may not persist beyond Perl 5.8.
The default algorithm is mergesort, which will be stable even if
you do not explicitly demand it.
But the stability of the default sort is a side-effect that could
change in later versions. If stability is important, be sure to
say so with a</p>
<pre>
<span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">'stable'</span><span class="operator">;</span>
</pre>
<p>The <code>no sort</code> pragma doesn't
<em>forbid</em> what follows, it just leaves the choice open. Thus, after</p>
<pre>
<span class="keyword">no</span> <span class="keyword">sort</span> <span class="string">qw(_mergesort stable)</span><span class="operator">;</span>
</pre>
<p>a mergesort, which happens to be stable, will be employed anyway.
Note that</p>
<pre>
<span class="keyword">no</span> <span class="keyword">sort</span> <span class="string">"_quicksort"</span><span class="operator">;</span>
<span class="keyword">no</span> <span class="keyword">sort</span> <span class="string">"_mergesort"</span><span class="operator">;</span>
</pre>
<p>have exactly the same effect, leaving the choice of sort algorithm open.</p>
<p>
</p>
<hr />
<h1><a name="caveats">CAVEATS</a></h1>
<p>This pragma is not lexically scoped: its effect is global to the program
it appears in. That means the following will probably not do what you
expect, because <em>both</em> pragmas take effect at compile time, before
<em>either</em> <a href="../lib/Pod/perlfunc.html#item_sort"><code>sort()</code></a> happens.</p>
<pre>
<span class="operator">{</span> <span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">"_quicksort"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">sort::current</span> <span class="operator">.</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="variable">@a</span> <span class="operator">=</span> <span class="keyword">sort</span> <span class="variable">@b</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">{</span> <span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">"stable"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">sort::current</span> <span class="operator">.</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="variable">@c</span> <span class="operator">=</span> <span class="keyword">sort</span> <span class="variable">@d</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="comment"># prints:</span>
<span class="comment"># quicksort stable</span>
<span class="comment"># quicksort stable</span>
</pre>
<p>You can achieve the effect you probably wanted by using <a href="../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>
to defer the pragmas until run time. Use the quoted argument
form of <a href="../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>, <em>not</em> the BLOCK form, as in</p>
<pre>
<span class="keyword">eval</span> <span class="operator">{</span> <span class="keyword">use</span> <span class="keyword">sort</span> <span class="string">"_quicksort"</span> <span class="operator">};</span> <span class="comment"># WRONG</span>
</pre>
<p>or the effect will still be at compile time.
Reset to default options before selecting other subpragmas
(in case somebody carelessly left them on) and after sorting,
as a courtesy to others.</p>
<pre>
<span class="operator">{</span> <span class="keyword">eval</span> <span class="string">'use sort qw(defaults _quicksort)'</span><span class="operator">;</span> <span class="comment"># force quicksort</span>
<span class="keyword">eval</span> <span class="string">'no sort "stable"'</span><span class="operator">;</span> <span class="comment"># stability not wanted</span>
<span class="keyword">print</span> <span class="variable">sort::current</span> <span class="operator">.</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="variable">@a</span> <span class="operator">=</span> <span class="keyword">sort</span> <span class="variable">@b</span><span class="operator">;</span>
<span class="keyword">eval</span> <span class="string">'use sort "defaults"'</span><span class="operator">;</span> <span class="comment"># clean up, for others</span>
<span class="operator">}</span>
<span class="operator">{</span> <span class="keyword">eval</span> <span class="string">'use sort qw(defaults stable)'</span><span class="operator">;</span> <span class="comment"># force stability</span>
<span class="keyword">print</span> <span class="variable">sort::current</span> <span class="operator">.</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="variable">@c</span> <span class="operator">=</span> <span class="keyword">sort</span> <span class="variable">@d</span><span class="operator">;</span>
<span class="keyword">eval</span> <span class="string">'use sort "defaults"'</span><span class="operator">;</span> <span class="comment"># clean up, for others</span>
<span class="operator">}</span>
<span class="comment"># prints:</span>
<span class="comment"># quicksort</span>
<span class="comment"># stable</span>
</pre>
<p>Scoping for this pragma may change in future versions.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -