5 $sort::hint_bits = 0x00020000; # HINT_LOCALIZE_HH, really...
7 $sort::quicksort_bit = 0x00000001;
8 $sort::mergesort_bit = 0x00000002;
9 $sort::sort_bits = 0x000000FF; # allow 256 different ones
10 $sort::stable_bit = 0x00000100;
11 $sort::insensitive_bit = 0x00000200;
12 $sort::safe_bits = 0x00000300;
13 $sort::fast_bit = 0x00000400;
21 Carp::croak("sort pragma requires arguments");
23 $^H |= $sort::hint_bits;
25 no warnings 'uninitialized'; # $^H{SORT} bitops would warn
26 while ($_ = shift(@_)) {
27 if (/^q(?:uick)?sort$/) {
28 $^H{SORT} &= ~$sort::sort_bits;
29 $^H{SORT} |= $sort::quicksort_bit;
31 } elsif ($_ eq 'mergesort') {
32 $^H{SORT} &= ~$sort::sort_bits;
33 $^H{SORT} |= $sort::mergesort_bit;
35 } elsif ($_ eq 'safe') {
36 $^H{SORT} &= ~$sort::fast_bit;
37 $^H{SORT} |= $sort::safe_bits;
40 } elsif ($_ eq 'fast') {
41 $^H{SORT} &= ~$sort::safe_bits;
42 $^H{SORT} |= $sort::fast_bit;
47 Carp::croak("sort: unknown subpragma '@_'");
55 push @sort, 'quicksort' if $^H{SORT} & $sort::quicksort_bit;
56 push @sort, 'mergesort' if $^H{SORT} & $sort::mergesort_bit;
57 push @sort, 'safe' if $^H{SORT} & $sort::safe_bits;
58 push @sort, 'fast' if $^H{SORT} & $sort::fast_bit;
60 push @sort, 'mergesort' unless @sort;
69 sort - perl pragma to control sort() behaviour
76 use sort 'qsort'; # alias for quicksort
78 # alias for mergesort: insensitive and stable
81 # alias for raw quicksort: sensitive and nonstable
84 my $current = sort::current();
88 With the sort pragma you can control the behaviour of the builtin
91 In Perl versions 5.6 and earlier the quicksort algorithm was used to
92 implement sort(), but in Perl 5.8 the algorithm was changed to mergesort,
93 mainly to guarantee insensitiveness to sort input: the worst case of
94 quicksort is O(N**2), while mergesort is always O(N log N).
96 On the other hand, for same cases (especially for shorter inputs)
99 In Perl 5.8 and later by default quicksort is wrapped into a
100 stabilizing layer. A stable sort means that for records that compare
101 equal, the original input ordering is preserved. Mergesort is stable;
104 The metapragmas 'fast' and 'safe' select quicksort without the
105 stabilizing layer and mergesort, respectively. In other words,
106 'safe' is the default.
108 Finally, the sort performance is also dependent on the platform
109 (smaller CPU caches favour quicksort).