-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdream_sort.pl
More file actions
75 lines (60 loc) · 1.7 KB
/
dream_sort.pl
File metadata and controls
75 lines (60 loc) · 1.7 KB
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
69
70
71
72
73
74
75
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 19 August 2025
# https://github.com/trizen
# A recursive sorting algorithm for strings, based on a dream that I had, similar to Radix sort.
# The running time of the algorithm is:
# O(n * len(s))
# where `n` is the number of strings being sorted and `s` is the longest string in the array.
# See also:
# https://en.wikipedia.org/wiki/Radix_sort
use 5.036;
use List::Util qw(shuffle);
use Test::More tests => 20;
sub dream_sort($arr, $i = 0) {
my @buckets;
foreach my $item (@$arr) {
my $byte = substr($item, $i, 1) // '';
if ($byte eq '') {
$byte = 0;
}
else {
$byte = ord($byte) + 1;
}
push @{$buckets[$byte]}, $item;
}
my @sorted;
if (defined($buckets[0])) {
push @sorted, @{$buckets[0]};
}
foreach my $k (1 .. $#buckets) {
my $entry = $buckets[$k];
if (defined($entry)) {
if (scalar(@$entry) == 1) {
push @sorted, $entry->[0];
}
else {
push @sorted, @{__SUB__->($entry, $i + 1)};
}
}
}
return \@sorted;
}
sub sort_test($arr) {
my @sorted = sort @$arr;
is_deeply(dream_sort($arr), \@sorted);
is_deeply(dream_sort([reverse @$arr]), \@sorted);
is_deeply(dream_sort(\@sorted), \@sorted);
is_deeply(dream_sort([shuffle(@$arr)]), \@sorted);
}
sort_test(["abc", "abd"]);
sort_test(["abc", "abc"]);
sort_test(["abcd", "abc"]);
sort_test(["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]);
sort_test(
do {
open my $fh, '<:raw', __FILE__;
local $/;
[split(' ', scalar <$fh>)];
}
);