June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,26 +1,21 @@
sub infix:<M> (@x, @y) {
gather {
while @x and @y {
take do given @x[0] cmp @y[0] {
when Increase { @x.shift }
when Decrease { @y.shift }
when Same { @x.shift, @y.shift }
}
}
take @x, @y;
}
sub infix:<M> (@x-in, @y-in) {
my @x = | @x-in;
my @y = | @y-in;
flat @x, @y,
reverse gather while @x and @y {
take do given @x[*-1] cmp @y[*-1] {
when More { pop @x }
when Less { pop @y }
when Same { pop(@x), pop(@y) }
}
}
}
sub strand (@x is rw) {
my $prev = -Inf;
sub strand (@x) {
my $i = 0;
my $prev = -Inf;
gather while $i < @x {
if @x[$i] before $prev {
$i++;
}
else {
take $prev = splice(@x, $i, 1)[0];
}
@x[$i] before $prev ?? $i++ !! take $prev = splice(@x, $i, 1)[0];
}
}
@ -31,11 +26,11 @@ sub strand_sort (@x is copy) {
}
my @a = (^100).roll(10);
say "Before @a[]";
say "Before {@a}";
@a = strand_sort(@a);
say "After @a[]";
say "After {@a}";
@a = <The quick brown fox jumps over the lazy dog>;
say "Before @a[]";
say "Before {@a}";
@a = strand_sort(@a);
say "After @a[]";
say "After {@a}";

View file

@ -0,0 +1,47 @@
# Project : Sorting algorithms/Strand sort
# Date : 2018/03/04
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
test = [-2,0,-2,5,5,3,-1,-3,5,5,0,2,-4,4,2]
results = []
resultsend = []
see "before sort:" + nl
showarray(test)
test = strandsort(test)
see "after sort:" + nl
showarray(test)
func strandsort(a)
while len(a) > 0
sublist = []
add(sublist,a[1])
del(a,1)
for i = 1 to len(a)
if a[i] > sublist[len(sublist)]
add(sublist,a[i])
del(a,i)
ok
next
for n = 1 to len(sublist)
add(results,sublist[n])
next
for n = 1 to len(results)
for m = n + 1 to len(results)
if results[m] < results[n]
temp = results[m]
results[m] = results[n]
results[n] = temp
ok
next
next
end
return results
func showarray(vect)
svect = ""
for n = 1 to len(vect)
svect = svect + vect[n] + " "
next
svect = left(svect, len(svect) - 1)
see svect + nl