1sort(3pm)              Perl Programmers Reference Guide              sort(3pm)
2
3
4

NAME

6       sort - perl pragma to control sort() behaviour
7

SYNOPSIS

9           use sort 'stable';          # guarantee stability
10           use sort '_quicksort';      # use a quicksort algorithm
11           use sort '_mergesort';      # use a mergesort algorithm
12           use sort 'defaults';        # revert to default behavior
13           no  sort 'stable';          # stability not important
14
15           use sort '_qsort';          # alias for quicksort
16
17           my $current = sort::current();      # identify prevailing algorithm
18

DESCRIPTION

20       With the "sort" pragma you can control the behaviour of the builtin
21       "sort()" function.
22
23       In Perl versions 5.6 and earlier the quicksort algorithm was used to
24       implement "sort()", but in Perl 5.8 a mergesort algorithm was also made
25       available, mainly to guarantee worst case O(N log N) behaviour: the
26       worst case of quicksort is O(N**2).  In Perl 5.8 and later, quicksort
27       defends against quadratic behaviour by shuffling large arrays before
28       sorting.
29
30       A stable sort means that for records that compare equal, the original
31       input ordering is preserved.  Mergesort is stable, quicksort is not.
32       Stability will matter only if elements that compare equal can be dis‐
33       tinguished in some other way.  That means that simple numerical and
34       lexical sorts do not profit from stability, since equal elements are
35       indistinguishable.  However, with a comparison such as
36
37          { substr($a, 0, 3) cmp substr($b, 0, 3) }
38
39       stability might matter because elements that compare equal on the first
40       3 characters may be distinguished based on subsequent characters.  In
41       Perl 5.8 and later, quicksort can be stabilized, but doing so will add
42       overhead, so it should only be done if it matters.
43
44       The best algorithm depends on many things.  On average, mergesort does
45       fewer comparisons than quicksort, so it may be better when complicated
46       comparison routines are used.  Mergesort also takes advantage of pre-
47       existing order, so it would be favored for using "sort()" to merge sev‐
48       eral sorted arrays.  On the other hand, quicksort is often faster for
49       small arrays, and on arrays of a few distinct values, repeated many
50       times.  You can force the choice of algorithm with this pragma, but
51       this feels heavy-handed, so the subpragmas beginning with a "_" may not
52       persist beyond Perl 5.8.  The default algorithm is mergesort, which
53       will be stable even if you do not explicitly demand it.  But the sta‐
54       bility of the default sort is a side-effect that could change in later
55       versions.  If stability is important, be sure to say so with a
56
57         use sort 'stable';
58
59       The "no sort" pragma doesn't forbid what follows, it just leaves the
60       choice open.  Thus, after
61
62         no sort qw(_mergesort stable);
63
64       a mergesort, which happens to be stable, will be employed anyway.  Note
65       that
66
67         no sort "_quicksort";
68         no sort "_mergesort";
69
70       have exactly the same effect, leaving the choice of sort algorithm
71       open.
72

CAVEATS

74       This pragma is not lexically scoped: its effect is global to the pro‐
75       gram it appears in.  That means the following will probably not do what
76       you expect, because both pragmas take effect at compile time, before
77       either "sort()" happens.
78
79         { use sort "_quicksort";
80           print sort::current . "\n";
81           @a = sort @b;
82         }
83         { use sort "stable";
84           print sort::current . "\n";
85           @c = sort @d;
86         }
87         # prints:
88         # quicksort stable
89         # quicksort stable
90
91       You can achieve the effect you probably wanted by using "eval()" to
92       defer the pragmas until run time.  Use the quoted argument form of
93       "eval()", not the BLOCK form, as in
94
95         eval { use sort "_quicksort" }; # WRONG
96
97       or the effect will still be at compile time.  Reset to default options
98       before selecting other subpragmas (in case somebody carelessly left
99       them on) and after sorting, as a courtesy to others.
100
101         { eval 'use sort qw(defaults _quicksort)'; # force quicksort
102           eval 'no sort "stable"';      # stability not wanted
103           print sort::current . "\n";
104           @a = sort @b;
105           eval 'use sort "defaults"';   # clean up, for others
106         }
107         { eval 'use sort qw(defaults stable)';     # force stability
108           print sort::current . "\n";
109           @c = sort @d;
110           eval 'use sort "defaults"';   # clean up, for others
111         }
112         # prints:
113         # quicksort
114         # stable
115
116       Scoping for this pragma may change in future versions.
117
118
119
120perl v5.8.8                       2001-09-21                         sort(3pm)
Impressum