blib/lib/Algorithm/Bitonic/Sort.pm

filter: Statementstmt 94.7% 72 / 76 Branchbran 85.0% 17 / 20 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 89.3% 109 / 122 CC 16 SCAR 14.8
CC
16
Cov
91%
_some_sorting_algorithm
6.6
_bitonic_merge
6.0
bitonic_sort
4.0
CRAP 4.4
↑ Summary
1 package Algorithm::Bitonic::Sort;
2  
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use constant DEBUG => $ENV{ALGORITHM_BITONIC_SORT_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 if (DEBUG) {
9 require Data::Dumper::Simple;
10 }
11  
12 3 our (@ISA, @EXPORT);
13 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 require Exporter;
15 3 @ISA = qw(Exporter);
16 3 @EXPORT = qw(bitonic_sort); # symbols to export on request
17 }
18  
19 # "A supercomputer is a device for turning compute-bound problems into I/O-bound problems."
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Algorithm::Bitonic::Sort - Sorting numbers with Bitonic Sort
26  
27 =head1 VERSION
28  
29 Version 0.06
30  
31 =cut
32  
33 3 our $VERSION = '0.06';
34  
35  
36 =head1 SYNOPSIS
37  
38 Use L<Algorithm::Bitonic::Sort> with the following style.
39  
40 use Algorithm::Bitonic::Sort;
41
42 my @sample = (1,4,8,4,4365,2,67,33,345);
43 my @result_inc = bitonic_sort( 1 ,@sample); # incremental
44 my @result_dec = bitonic_sort( 0 ,@sample); # decremental
45  
46 =head1 DESCRIPTION
47  
48 Bitonic mergesort is a parallel algorithm for sorting. It is also used as a construction method for building a sorting network.
49 This is an Perl 5 implementation of Ken Batcher's Bitonic mergesort.
50  
51 =head1 Limitation
52  
53 This is a enhanced version of Bitonic Sort which removed the limitation of original version.
54 This module supports any amount of numbers.
55  
56 The original Bitonic can only sort N numbers, which N is a power of 2.
57  
58  
59 =head1 EXPORT
60  
61 bitonic_sort
62  
63  
64 =head1 SUBROUTINES
65  
66 =head2 bitonic_sort
67  
68 The First Parameter works as the ascending/decreasing selector.
69 True (1 or any true value) means ascending (incremental),
70 False (0 or any false value) means decreasing.
71  
72 All other params will be treated as members/items to be sorted.
73  
74  
75 =cut
76  
77 30 ▶sub bitonic_sort {
Subroutine bitonic_sort: called CC=4 SCAR=13.9
Pod documented
78 30 my $up = shift;
79 30 say '#### Sort: '.Dumper(@_) if DEBUG;
80
81 30 ▶ return @_ if int @_ <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if int @_ <= 1 16 14 30
82
83 14 ▶ my $single_bit = shift @_ if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 2 12 14
84 14 ▶ $single_bit //= 'NA';
Condition100%2/2
$single_bit //= "NA"
l!l
122
MC/DC100%1/1
$single_bit //= "NA"
$single_bit
Truth table100%2/2
A
A= $single_bit
Aresult
11
00
85
86 14 say Dumper $single_bit if DEBUG;
87
88 14 my @num = @_;
89 14 my @first = bitonic_sort( 1, @num[0..(@num /2 -1)] );
90 14 my @second = bitonic_sort( 0, @num[(@num /2)..(@num -1)] );
91
92 14 return _bitonic_merge( $up, $single_bit, @first, @second );
93 }
94  
95 82 ▶sub _bitonic_merge {
Subroutine _bitonic_merge: called CC=6 SCAR=17.9
96 82 my $up = shift;
97 82 say '#### Merge: '.Dumper(@_) if DEBUG;
98
99 82 my $single_bit = shift;
100 82 say Dumper $single_bit if DEBUG;
101
102 # assume input @num is bitonic, and sorted list is returned
103 82 ▶ return @_ if int @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if int @_ == 1 48 34 82
104
105 34 ▶ my $single_bit_2 = shift @_ if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 34 34
106 34 ▶ $single_bit_2 //= 'NA';
Condition100%2/2
$single_bit_2 //= "NA"
l!l
313
MC/DC100%1/1
$single_bit_2 //= "NA"
$single_bit_2
Truth table100%2/2
A
A= $single_bit_2
Aresult
11
00
107
108 34 my @num = @_;
109 34 @num = _bitonic_compare( $up, @num );
110
111 34 my @first = _bitonic_merge( $up, 'NA', @num[0..(@num /2 -1)] );
112 34 my @second = _bitonic_merge( $up, 'NA', @num[(@num /2)..(@num -1)] );
113
114 34 @num = (@first, @second);
115 34 ▶ @num = _some_sorting_algorithm( $up, $single_bit, @first, @second ) if $single_bit ne 'NA';
Branch: 1 branch
BranchTrueFalseTotal
if $single_bit ne "NA" 2 32 34
116 34 ▶ @num = _some_sorting_algorithm( $up, $single_bit_2, @first, @second ) if $single_bit_2 ne 'NA';
Branch: 1 branch
BranchTrueFalseTotal
if $single_bit_2 ne "NA" 0 34 34
117
118 34 say "#####\n# Merge Result\n#####\n".Dumper(@num) if DEBUG;
119
120 34 return (@num);
121 }
122  
123 34 ▶sub _bitonic_compare {
Subroutine _bitonic_compare: called CC=3 SCAR=11.0
124 34 my $up = shift;
125 34 say '#### Compare: '.Dumper(@_) if DEBUG;
126 34 my @num = @_;
127
128 34 my $dist = int @num /2;
129 #~
130 34 for my $i (0..$dist-1) {
131 48 say "i=$i, dist=$dist, $num[$i] > $num[$i+$dist]) == $up" if DEBUG;
132 48 ▶ if ( ($num[$i] > $num[$i+$dist]) == $up ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num[$i] > $num[$i + $dist] == $up) 26 22 48
133 26 say "Swapping....." if DEBUG;
134 26 ($num[$i], $num[$i+$dist]) = ($num[$i+$dist], $num[$i]); #swap
135 }
136 }
137 #~ for my $i (0..(int @$first)) {
138 #~ if ( ($first->[$i] > $second->[$i]) == $up ) {
139 #~ ($first->[$i], $second->[$i]) = ($second->[$i], $first->[$i]); #swap
140 #~ }
141 #~ }
142
143 34 say 'Compared result:'.Dumper(@num) if DEBUG;
144 34 return @num;
145 #~ return ($first, $second);
146 }
147  
148  
149 2 ▶sub _some_sorting_algorithm {
Subroutine _some_sorting_algorithm: called CC=6 SCAR=18.8
150 2 my $up = shift;
151 2 my $single_bit = shift;
152 2 my @num = @_;
153 2 my @num_new;
154
155 2 say "_SOME_SORTING_ALGORITHM: INPUT: ".Dumper(@num) if DEBUG;
156
157 2 ▶ while (my $curr = shift @num) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $curr = shift @num) 9 1 10
158 9 say "_SOME_SORTING_ALGORITHM: for: ".Dumper($curr, $single_bit, @num) if DEBUG;
159 9 ▶ if ($up and $single_bit < $curr) {
Branch: 2 branches
BranchTrueFalseTotal
if ($up and $single_bit < $curr) { } 1 8 9
elsif ($single_bit > $curr and not $up) { } 0 8 8
Condition66%2/3
$up and $single_bit < $curr
!ll&&!rl&&r
801
MC/DC50%1/2
$up and $single_bit < $curr
$up $single_bit < $curr
Truth table66%2/3
A and B
A= $upB= $single_bit < $curr
ABresult
0X0
100
111
160 1 push @num_new, $single_bit;
161 1 push @num_new, $curr;
162 1 say "Return earlier, up is ".($up or '0').':'.Dumper(@num_new, @num) if DEBUG;
163 1 return (@num_new, @num);
164 ▶ } elsif ($single_bit > $curr and not $up) {
Condition33%1/3
$single_bit > $curr and not $up
!ll&&!rl&&r
800
MC/DC0%0/2
$single_bit > $curr and not $up
$single_bit > $curr not $up
Truth table33%1/3
A and not(B)
A= $single_bit > $currB= not $up
ABresult
0X0
100
111
165 0 push @num_new, $single_bit;
166 0 push @num_new, $curr;
167 0 say "Return earlier, up is ".($up or '0').':'.Dumper(@num_new, @num) if DEBUG;
168 0 return (@num_new, @num)
169 } else {
170 8 push @num_new, $curr;
171 }
172 }
173
174 1 push @num_new, $single_bit;
175 1 say "Return normal, ".Dumper(@num_new, @num) if DEBUG;
176 1 return @num_new;
177 }
178  
179 =head1 AUTHOR
180  
181 BlueT - Matthew Lien - 練喆明, C<< <BlueT at BlueT.org> >>
182  
183  
184 =head1 INSTALLATION
185  
186 To install this module, run the following commands:
187  
188 perl Makefile.PL
189 make
190 make test
191 make install
192  
193 Or install with cpanm
194  
195 cpanm Algorithm::Bitonic::Sort
196  
197  
198 =head1 BUGS
199  
200 Please report any bugs or feature requests to CPAN ( C<bug-algorithm-bitonic-sort at rt.cpan.org>, L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Algorithm-Bitonic-Sort> ) or GitHub (L<https://github.com/BlueT/Algorithm-Bitonic-Sort/issues>).
201 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
202  
203  
204  
205  
206 =head1 SUPPORT
207  
208 You can find documentation for this module with the perldoc command.
209  
210 perldoc Algorithm::Bitonic::Sort
211  
212  
213 You can also look for information at:
214  
215 =over 4
216  
217 =item * RT: CPAN's request tracker (report bugs here)
218  
219 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Algorithm-Bitonic-Sort>
220  
221 =item * AnnoCPAN: Annotated CPAN documentation
222  
223 L<http://annocpan.org/dist/Algorithm-Bitonic-Sort>
224  
225 =item * CPAN Ratings
226  
227 L<http://cpanratings.perl.org/d/Algorithm-Bitonic-Sort>
228  
229 =item * Search CPAN
230  
231 L<http://search.cpan.org/dist/Algorithm-Bitonic-Sort/>
232  
233 =item * Launchpad
234  
235 L<https://launchpad.net/p5-algorithm-bitonic-sort>
236  
237 =item * GitHub
238  
239 L<https://github.com/BlueT/Algorithm-Bitonic-Sort>
240  
241 =back
242  
243  
244 =head1 ACKNOWLEDGEMENTS
245  
246 =head1 SEE ALSO
247  
248 =over 4
249  
250 =item * Batcher's web page at Kent State University
251  
252 L<http://www.cs.kent.edu/~batcher/>
253  
254 =item * Bitonic sorter on Wikipedia
255  
256 L<http://en.wikipedia.org/wiki/Bitonic_sorter>
257  
258 =back
259  
260 =head1 LICENSE AND COPYRIGHT
261  
262 Copyright 2012-2017 BlueT - Matthew Lien - 練喆明.
263  
264 This program is free software; you can redistribute it and/or modify it
265 under the terms of either: the GNU General Public License as published
266 by the Free Software Foundation; or the Artistic License.
267  
268 See http://dev.perl.org/licenses/ for more information.
269  
270  
271 =cut
272  
273 3 1; # End of Algorithm::Bitonic::Sort
↑ Summary