Data update

This commit is contained in:
Ingy döt Net 2024-03-06 22:25:12 -08:00
parent ed705008a8
commit 0df55f9f24
2196 changed files with 32999 additions and 3075 deletions

View file

@ -6,10 +6,10 @@ MODULE RosettaMergeSort;
(* Return TRUE if `front` comes before `rear` in the sorted order, FALSE otherwise *)
(* For the sort to be stable `front` comes before `rear` if they are equal *)
PROCEDURE (IN t: Template) Before- (front, rear: ANYPTR): BOOLEAN, NEW, ABSTRACT;
PROCEDURE (IN t: Template) Pre- (front, rear: ANYPTR): BOOLEAN, NEW, ABSTRACT;
(* Return the next element in the list after `s` *)
PROCEDURE (IN t: Template) Next- (s: ANYPTR): ANYPTR, NEW, ABSTRACT;
PROCEDURE (IN t: Template) Suc- (s: ANYPTR): ANYPTR, NEW, ABSTRACT;
(* Update the next pointer of `s` to the value of `next` - Return the modified `s` *)
PROCEDURE (IN t: Template) Set- (s, next: ANYPTR): ANYPTR, NEW, ABSTRACT;
@ -18,12 +18,12 @@ MODULE RosettaMergeSort;
PROCEDURE (IN t: Template) Length* (s: ANYPTR): INTEGER, NEW;
VAR n: INTEGER;
BEGIN
n := 0; (* Initialize the count of elements to 0 *)
WHILE s # NIL DO (* While not at the end of the list *)
INC(n); (* Increment the count of elements *)
s := t.Next(s) (* Move to the next element in the linked list *)
n := 0;
WHILE s # NIL DO
INC(n);
s := t.Suc(s)
END;
RETURN n (* Return the total number of elements in the linked list *)
RETURN n
END Length;
(* Merge sorted lists `front` and `rear` - Return the merged sorted list *)
@ -31,10 +31,10 @@ MODULE RosettaMergeSort;
BEGIN
IF front = NIL THEN RETURN rear END;
IF rear = NIL THEN RETURN front END;
IF t.Before(front, rear) THEN
RETURN t.Set(front, t.Merge(t.Next(front), rear))
IF t.Pre(front, rear) THEN
RETURN t.Set(front, t.Merge(t.Suc(front), rear))
ELSE
RETURN t.Set(rear, t.Merge(front, t.Next(rear)))
RETURN t.Set(rear, t.Merge(front, t.Suc(rear)))
END
END Merge;
@ -48,17 +48,17 @@ MODULE RosettaMergeSort;
VAR k: INTEGER; h, front, rear: ANYPTR;
BEGIN
IF n = 1 THEN (* base case: if n = 1, return the head of `s` *)
h := s; s := t.Next(s); RETURN t.Set(h, NIL)
h := s; s := t.Suc(s); RETURN t.Set(h, NIL)
END;
(* Divide the first n elements of the list into two sorted halves *)
k := n DIV 2;
front := TakeSort(k, s);
front := TakeSort(k, s);
rear := TakeSort(n - k, s);
RETURN t.Merge(front, rear) (* Merge and return the halves *)
END TakeSort;
BEGIN
IF s = NIL THEN RETURN s END; (* If `s` in empty, return `s` *)
IF s = NIL THEN RETURN NIL END; (* If `s` in empty, return `s` *)
(* Calculate the length of the list and call TakeSort *)
RETURN TakeSort(t.Length(s), s) (* Return the sorted list *)
END Sort;

View file

@ -1,7 +1,9 @@
MODULE RosettaMergeSortUse;
(* Import Modules: *)
IMPORT Sort := RosettaMergeSort, Log := StdLog;
IMPORT Sort := RosettaMergeSort, Out;
(* Type Definitions: *)
TYPE
@ -52,10 +54,10 @@ MODULE RosettaMergeSortUse;
count := 0;
WHILE s # NIL DO
IF count = 10 THEN
Log.Ln; (* Insert a newline after displaying 10 numbers *)
Out.Ln; (* Insert a newline after displaying 10 numbers *)
count := 0
END;
Log.IntForm(s.value, Log.decimal, 4, ' ', Log.hideBase);
Out.Int(s.value, 4);
s := s.next;
INC(count)
END
@ -75,11 +77,11 @@ MODULE RosettaMergeSortUse;
b(130); b(866); b(937); b(226); b(298); b(029); b(149); b(381);
b(590); b(255); b(101); b(485); b(801); b(223); b(645); b(458);
b(068); b(683);
Log.String("Before:"); Log.Ln;
Show(s); Log.Ln;
Out.String("Before:"); Out.Ln;
Show(s); Out.Ln;
s := t.Sort(s)(List);
Log.String("After:"); Log.Ln;
Show(s); Log.Ln
Out.String("After:"); Out.Ln;
Show(s); Out.Ln
END Use;
END RosettaMergeSortUse.
END RosettaMergeSortUse.Use