-
Notifications
You must be signed in to change notification settings - Fork 33
/
binsertion_sorting_algorithm.pl
executable file
·68 lines (49 loc) · 1.39 KB
/
binsertion_sorting_algorithm.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
# Author: Trizen
# Date: 16 December 2013
# Edit: 06 December 2023
# https://github.com/trizen
# Sorting algorithm: insertion sort + binary search = binsertion sort
use 5.036;
use strict;
use warnings;
sub bsearch_ge ($left, $right, $callback) {
my ($mid, $cmp);
for (; ;) {
$mid = ($left + $right) >> 1;
$cmp = $callback->($mid) || return $mid;
if ($cmp < 0) {
$left = $mid + 1;
if ($left > $right) {
$mid += 1;
last;
}
}
else {
$right = $mid - 1;
$left > $right and last;
}
}
return $mid;
}
sub binsertion_sort {
my (@list) = @_;
foreach my $i (1 .. $#list) {
if ((my $k = $list[$i]) < $list[$i - 1]) {
splice(@list, $i, 1);
splice(@list, bsearch_ge(0, $i - 1, sub ($j) { $list[$j] <=> $k }), 0, $k);
}
}
return @list;
}
#
## MAIN
#
use List::Util qw(shuffle);
my @list = (shuffle((1 .. 100) x 2))[0 .. 50];
say "Before: ", join(' ', @list);
say "After: ", join(' ', binsertion_sort(@list));
my @sorted = sort { $a <=> $b } @list;
join(' ', binsertion_sort(@list)) eq join(' ', @sorted) or die "error";
join(' ', binsertion_sort(@sorted)) eq join(' ', @sorted) or die "error";
join(' ', binsertion_sort(reverse @sorted)) eq join(' ', @sorted) or die "error";