Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,28 @@
Selection: procedure options (main); /* 2 November 2013 */
declare a(10) fixed b inary initial (
5, 7, 3, 98, 4, -3, 25, 20, 60, 17);
put edit (trim(a)) (a, x(1));
call Selection_Sort (a);
put skip edit (trim(a)) (a, x(1));
Selection_sort: procedure (a);
declare a(*) fixed binary;
declare t fixed binary;
declare n fixed binary;
declare (i, j, k) fixed binary;
n = hbound(a,1);
do j = 1 to n;
k = j; t = a(j);
do i = j+1 to n;
if t > a(i) then do; t = a(i); k = i; end;
end;
a(k) = a(j); a(j) = t;
end;
end Selection_Sort;
end Selection;

View file

@ -1,18 +1,15 @@
class Array
def selectionsort!
0.upto(length - 2) do |i|
(min_idx = i + 1).upto(length - 1) do |j|
if self[j] < self[min_idx]
min_idx = j
end
end
if self[i] > self[min_idx]
self[i], self[min_idx] = self[min_idx], self[i]
for i in 0..length-2
min_idx = i
for j in (i+1)...length
min_idx = j if self[j] < self[min_idx]
end
self[i], self[min_idx] = self[min_idx], self[i]
end
self
end
end
ary = [7,6,5,9,8,4,3,1,2,0]
ary.selectionsort!
p ary.selectionsort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]