1sort(3pm) Perl Programmers Reference Guide sort(3pm)
2
3
4
6 sort - perl pragma to control sort() behaviour
7
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;
18 BEGIN {
19 $current = sort::current(); # identify prevailing algorithm
20 }
21
23 With the "sort" pragma you can control the behaviour of the builtin
24 "sort()" function.
25
26 In Perl versions 5.6 and earlier the quicksort algorithm was used to
27 implement "sort()", but in Perl 5.8 a mergesort algorithm was also made
28 available, mainly to guarantee worst case O(N log N) behaviour: the
29 worst case of quicksort is O(N**2). In Perl 5.8 and later, quicksort
30 defends against quadratic behaviour by shuffling large arrays before
31 sorting.
32
33 A stable sort means that for records that compare equal, the original
34 input ordering is preserved. Mergesort is stable, quicksort is not.
35 Stability will matter only if elements that compare equal can be
36 distinguished in some other way. That means that simple numerical and
37 lexical sorts do not profit from stability, since equal elements are
38 indistinguishable. However, with a comparison such as
39
40 { substr($a, 0, 3) cmp substr($b, 0, 3) }
41
42 stability might matter because elements that compare equal on the first
43 3 characters may be distinguished based on subsequent characters. In
44 Perl 5.8 and later, quicksort can be stabilized, but doing so will add
45 overhead, so it should only be done if it matters.
46
47 The best algorithm depends on many things. On average, mergesort does
48 fewer comparisons than quicksort, so it may be better when complicated
49 comparison routines are used. Mergesort also takes advantage of pre-
50 existing order, so it would be favored for using "sort()" to merge
51 several sorted arrays. On the other hand, quicksort is often faster
52 for small arrays, and on arrays of a few distinct values, repeated many
53 times. You can force the choice of algorithm with this pragma, but
54 this feels heavy-handed, so the subpragmas beginning with a "_" may not
55 persist beyond Perl 5.8. The default algorithm is mergesort, which
56 will be stable even if you do not explicitly demand it. But the
57 stability of the default sort is a side-effect that could change in
58 later versions. If stability is important, be sure to say so with a
59
60 use sort 'stable';
61
62 The "no sort" pragma doesn't forbid what follows, it just leaves the
63 choice open. Thus, after
64
65 no sort qw(_mergesort stable);
66
67 a mergesort, which happens to be stable, will be employed anyway. Note
68 that
69
70 no sort "_quicksort";
71 no sort "_mergesort";
72
73 have exactly the same effect, leaving the choice of sort algorithm
74 open.
75
77 As of Perl 5.10, this pragma is lexically scoped and takes effect at
78 compile time. In earlier versions its effect was global and took effect
79 at run-time; the documentation suggested using "eval()" to change the
80 behaviour:
81
82 { eval 'use sort qw(defaults _quicksort)'; # force quicksort
83 eval 'no sort "stable"'; # stability not wanted
84 print sort::current . "\n";
85 @a = sort @b;
86 eval 'use sort "defaults"'; # clean up, for others
87 }
88 { eval 'use sort qw(defaults stable)'; # force stability
89 print sort::current . "\n";
90 @c = sort @d;
91 eval 'use sort "defaults"'; # clean up, for others
92 }
93
94 Such code no longer has the desired effect, for two reasons. Firstly,
95 the use of "eval()" means that the sorting algorithm is not changed
96 until runtime, by which time it's too late to have any effect.
97 Secondly, "sort::current" is also called at run-time, when in fact the
98 compile-time value of "sort::current" is the one that matters.
99
100 So now this code would be written:
101
102 { use sort qw(defaults _quicksort); # force quicksort
103 no sort "stable"; # stability not wanted
104 my $current;
105 BEGIN { $current = print sort::current; }
106 print "$current\n";
107 @a = sort @b;
108 # Pragmas go out of scope at the end of the block
109 }
110 { use sort qw(defaults stable); # force stability
111 my $current;
112 BEGIN { $current = print sort::current; }
113 print "$current\n";
114 @c = sort @d;
115 }
116
117
118
119perl v5.12.4 2011-06-01 sort(3pm)