Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -0,0 +1,151 @@
[Bubble sort demo for Rosetta Code website]
[EDSAC program. Initial Orders 2]
[Sorts a list of double-word integers.
List must be loaded at an even address.
First item gives number of items to follow.
Address of list is placed in location 49.
List can then be referred to with code letter L.]
T49K
P300F [<---------- address of list here]
[Subroutine R2, reads positive integers during input of orders.
Items separated by F; list ends with #TZ.]
GKT20FVDL8FA40DUDTFI40FA40FS39FG@S2FG23FA5@T5@E4@E13Z
[Tell R2 where to store integers it reads from tape.]
T #L ['T m D' in documentation, but this also works]
[Lists of integers, comment out all except one]
[10 integers from digits of pi]
10F314159F265358F979323F846264F338327F950288F419716F939937F510582F097494#TZ
[32 integers from digits of e ]
[32F
27182818F28459045F23536028F74713526F62497757F24709369F99595749F66967627F
72407663F03535475F94571382F17852516F64274274F66391932F00305992F18174135F
96629043F57290033F42952605F95630738F13232862F79434907F63233829F88075319F
52510190F11573834F18793070F21540891F49934884F16750924F47614606F68082264#TZ]
[Library subroutine P7, prints positive integer at 0D.
35 locations; load at aneven address.]
T 56 K
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSFL4F
T4DA1FA27@G11@XFT28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@
[The EDSAC code below implements the following Pascal program,
where the integers to be sorted are in a 1-based array x.
Since the assembler used (EdsacPC by Martin Campbell-Kelly)
doesn't allow square brackets inside comments, they are
replaced here by curly brackets.]
[
swapped := true;
j := n; // number of items
while (swapped and (j >= 2)) do begin
swapped := false;
for i := 1 to j - 1 do begin
// Using temp in the comparison makes the EDSAC code a bit simpler
temp := x{i};
if (x{i + 1} < temp) then begin
x{i} := x{i + 1};
x{i + 1} := temp;
swapped := true;
end;
end;
dec(j);
end;
]
[Main routine]
T 100 K
G K
[0] P F P F [double-word temporary store]
[2] P F [flag for swapped, >= 0 if true, < 0 if false]
[3] P F ['A' order for x{j}; implicitly defines j]
[4] P 2 F [to change list index by 1, i.e.change address by 2]
[5] A #L ['A' order for number of items]
[6] A 2#L ['A' order for x{1}]
[7] A 4#L ['A' order for x{2}]
[8] I2046 F [add to convert 'A' order to 'T' and dec address by 2]
[9] K4096 F [(1) minimum 17-bit value (2) teleprinter null]
[10] P D [constant 1, used in printing]
[11] # F [figure shift]
[12] & F [line feed]
[13] @ F [carriage return]
[Enter here with acc = 0]
[14] T 2 @ [swapped := true]
A L [get count, n in Pascal program above]
L 1 F [times 4 by shifting]
A 5 @ [make 'A' order for x{n}; initializes j := n]
[Start 'while' loop of Pascal program.
Here acc = 'A' order for x{j}]
[18] U 3 @ [update j]
S 7 @ [subtract 'A' order for x{2}]
G 56 @ [if j < 2 then done]
T F [acc := 0]
A 2 @ [test for swapped, acc >= 0 if so]
G 56 @ [if not swapped then done]
A 9 @ [change acc from >= 0 to < 0]
T 2 @ [swapped := false until swap occurs]
A 6 @ ['A' order for x{1}; initializes i := 1]
[Start 'for' loop of Pascal program.
Here acc = 'A' order for x{i}]
[27] U 36 @ [store order]
S 3 @ [subtract 'A' order for x{j}]
E 52 @ [out of 'for' loop if i >= j]
T F [clear acc]
A 36 @ [load 'A' order for x{i}]
A 4 @ [inc address by 2]
U 38 @ [plant 'A' order for x{i + 1}]
A 8 @ ['A' to 'T', and dec address by 2]
T 42 @ [plant 'T' order for x{i}]
[36] A #L [load x{i}; this order implicitly defines i]
T #@ [temp := x{i}]
[38] A #L [load x{i + 1}]
S #@ [acc := x{i + 1} - temp]
E 49 @ [don't swap if x{i + 1} >= temp]
[Here to swap x{i} and x{i + 1}]
A #@ [restore acc := x{i + 1} after test]
[42] T #L [x{i} := x{i + 1}]
A 42 @ [load 'T' order for x{i}]
A 4 @ [inc address by 2]
T 47 @ [plant 'T' order for x{i + 1}]
A #@ [load temp]
[47] T #L [to x{i + 1}]
T 2 @ [swapped := 0 (true)]
[49] T F [clear acc]
A 38 @ [load 'A' order for x{i + 1}]
G 27 @ [loop (unconditional) to inc i]
[52] T F
A 3 @ [load 'A' order for x{j}]
S 4 @ [dec address by 2]
G 18 @ [loop (unconditional) to dec j]
[Print the sorted list of integers]
[56] O 11 @ [figure shift]
T F [clear acc]
A 5 @ [load 'A' order for head of list]
T 65 @ [plant in code below]
S L [load negative number of items]
[61] T @ [use first word of temp store for count]
A 65 @ [load 'A' order for item]
A 4 @ [inc address by 2]
T 65 @ [store back]
[65] A #L [load next item in list]
T D [to 0D for printing]
[67] A 67 @ [for subroutine return]
G 56 F [print integer, clears acc]
O 13 @ [print CR]
O 12 @ [print LF]
A @ [negative count]
A 10 @ [add 1]
G 61 @ [loop back till count = 0]
[74] O 9 @ [null to flush teleprinter buffer]
Z F [stop]
E 14 Z [define entry point]
P F [acc = 0 on entry]

View file

@ -29,6 +29,6 @@ extension op
public program()
{
var list := new int[]{3, 7, 3, 2, 1, -4, 10, 12, 4};
var list := new int[]::(3, 7, 3, 2, 1, -4, 10, 12, 4);
console.printLine(list.bubbleSort().asEnumerable())
}

View file

@ -0,0 +1,39 @@
class BubbleSort {
@:generic
public static function sort<T>(arr:Array<T>) {
var madeChanges = false;
var itemCount = arr.length;
do {
madeChanges = false;
itemCount--;
for (i in 0...itemCount) {
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
var temp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = temp;
madeChanges = true;
}
}
} while (madeChanges);
}
}
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
BubbleSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
BubbleSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
BubbleSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}

View file

@ -6,12 +6,12 @@ call bSort # /*invoke the bubble sort with
call show ' after sort' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg n; m=n-1 /*N: is the number of @ array elements.*/
do m=m for m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/
end /*j*/
end /*m*/; return
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/
do m=n-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j<=@.k then iterate /*elements in order? */
_=@.j; @.j=@.k; @.k=_; ok=0 /*swap two elements; flag as not done.*/
end /*j*/
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = '---letters of the Hebrew alphabet---' ; @.13= "kaph [kaf]"
@.2 = '====================================' ; @.14= "lamed"

View file

@ -6,8 +6,8 @@ call bSort N /*invoke the bubble sort with
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg n; m=n-1 /*N: is the number of @ array elements.*/
do m=m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
bSort: procedure expose @.; parse arg n /*N: is the number of @ array elements.*/
do m=n-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1; if @.j>@.k then parse value @.j @.k 0 with @.k @.j ok
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
end /*m*/; return

View file

@ -10,14 +10,14 @@ call bSort N /*invoke the bubble sort with
call show ' after sort:' /*show the after array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
bSort: procedure expose @.; parse arg #; m=#-1 /*N: is the number of @ array elements.*/
bSort: procedure expose @.; parse arg # /*N: is the number of @ array elements.*/
call disp /*show a snapshot of the unsorted array*/
do m=m by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do m=#-1 by -1 until ok; ok=1 /*keep sorting the @ array until done.*/
do j=1 for m; k=j+1
if @.j>@.k then do; parse value @.j @.k 0 with @.k @.j ok
end
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
call disp /*show snapshot of partically sorted @.*/
call disp /*show snapshot of partially sorted @. */
end /*m*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: do j=1 for N; @.j= j; end

View file

@ -1,4 +1,4 @@
func bubbleSort<T:Comparable>(inout list:[T]) {
func bubbleSort<T:Comparable>(list:inout[T]) {
var done = false
while !done {
done = true
@ -10,3 +10,8 @@ func bubbleSort<T:Comparable>(inout list:[T]) {
}
}
}
var list1 = [3, 1, 7, 5, 2, 5, 3, 8, 4]
print(list1)
bubbleSort(list: &list1)
print(list1)

View file

@ -0,0 +1,29 @@
void swap(int[] array, int i1, int i2) {
if (array[i1] == array[i2])
return;
var tmp = array[i1];
array[i1] = array[i2];
array[i2] = tmp;
}
void bubble_sort(int[] array) {
bool flag = true;
int j = array.length;
while(flag) {
flag = false;
for (int i = 1; i < j; i++) {
if (array[i] < array[i - 1]) {
swap(array, i - 1, i);
flag = true;
}
}
j--;
}
}
void main() {
int[] array = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3};
bubble_sort(array);
foreach (int i in array)
print("%d ", i);
}