Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,9 +0,0 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
type Index_Type is (<>);
|
||||
type Collection_Type is array(Index_Type range <>) of Element_Type;
|
||||
with function "<"(Left, Right : Element_Type) return Boolean is <>;
|
||||
|
||||
package Mergesort is
|
||||
function Sort(Item : Collection_Type) return Collection_Type;
|
||||
end MergeSort;
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
package body Mergesort is
|
||||
|
||||
-----------
|
||||
-- Merge --
|
||||
-----------
|
||||
|
||||
function Merge(Left, Right : Collection_Type) return Collection_Type is
|
||||
Result : Collection_Type(Left'First..Right'Last);
|
||||
Left_Index : Index_Type := Left'First;
|
||||
Right_Index : Index_Type := Right'First;
|
||||
Result_Index : Index_Type := Result'First;
|
||||
begin
|
||||
while Left_Index <= Left'Last and Right_Index <= Right'Last loop
|
||||
if Left(Left_Index) <= Right(Right_Index) then
|
||||
Result(Result_Index) := Left(Left_Index);
|
||||
Left_Index := Index_Type'Succ(Left_Index); -- increment Left_Index
|
||||
else
|
||||
Result(Result_Index) := Right(Right_Index);
|
||||
Right_Index := Index_Type'Succ(Right_Index); -- increment Right_Index
|
||||
end if;
|
||||
Result_Index := Index_Type'Succ(Result_Index); -- increment Result_Index
|
||||
end loop;
|
||||
if Left_Index <= Left'Last then
|
||||
Result(Result_Index..Result'Last) := Left(Left_Index..Left'Last);
|
||||
end if;
|
||||
if Right_Index <= Right'Last then
|
||||
Result(Result_Index..Result'Last) := Right(Right_Index..Right'Last);
|
||||
end if;
|
||||
return Result;
|
||||
end Merge;
|
||||
|
||||
----------
|
||||
-- Sort --
|
||||
----------
|
||||
|
||||
function Sort (Item : Collection_Type) return Collection_Type is
|
||||
Result : Collection_Type(Item'range);
|
||||
Middle : Index_Type;
|
||||
begin
|
||||
if Item'Length <= 1 then
|
||||
return Item;
|
||||
else
|
||||
Middle := Index_Type'Val((Item'Length / 2) + Index_Type'Pos(Item'First));
|
||||
declare
|
||||
Left : Collection_Type(Item'First..Index_Type'Pred(Middle));
|
||||
Right : Collection_Type(Middle..Item'Last);
|
||||
begin
|
||||
for I in Left'range loop
|
||||
Left(I) := Item(I);
|
||||
end loop;
|
||||
for I in Right'range loop
|
||||
Right(I) := Item(I);
|
||||
end loop;
|
||||
Left := Sort(Left);
|
||||
Right := Sort(Right);
|
||||
Result := Merge(Left, Right);
|
||||
end;
|
||||
return Result;
|
||||
end if;
|
||||
end Sort;
|
||||
|
||||
end Mergesort;
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Mergesort;
|
||||
|
||||
procedure Mergesort_Test is
|
||||
type List_Type is array(Positive range <>) of Integer;
|
||||
package List_Sort is new Mergesort(Integer, Positive, List_Type);
|
||||
procedure Print(Item : List_Type) is
|
||||
begin
|
||||
for I in Item'range loop
|
||||
Put(Integer'Image(Item(I)));
|
||||
end loop;
|
||||
New_Line;
|
||||
end Print;
|
||||
|
||||
List : List_Type := (1, 5, 2, 7, 3, 9, 4, 6);
|
||||
begin
|
||||
Print(List);
|
||||
Print(List_Sort.Sort(List));
|
||||
end Mergesort_Test;
|
||||
|
|
@ -19,11 +19,10 @@ merge: function [a,b,left,middle,right][
|
|||
i: left
|
||||
|
||||
while [and? l < leftLen r < leftLen + rightLen][
|
||||
if? b\[l] < b\[r] [
|
||||
switch b\[l] < b\[r] [
|
||||
a\[i]: b\[l]
|
||||
l: l + 1
|
||||
]
|
||||
else [
|
||||
][
|
||||
a\[i]: b\[r]
|
||||
r: r + 1
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,293 +0,0 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. MERGESORT.
|
||||
AUTHOR. DAVE STRATFORD.
|
||||
DATE-WRITTEN. APRIL 2010.
|
||||
INSTALLATION. HEXAGON SYSTEMS LIMITED.
|
||||
******************************************************************
|
||||
* MERGE SORT *
|
||||
* The Merge sort uses a completely different paradigm, one of *
|
||||
* divide and conquer, to many of the other sorts. The data set *
|
||||
* is split into smaller sub sets upon which are sorted and then *
|
||||
* merged together to form the final sorted data set. *
|
||||
* This version uses the recursive method. Split the data set in *
|
||||
* half and perform a merge sort on each half. This in turn splits*
|
||||
* each half again and again until each set is just one or 2 items*
|
||||
* long. A set of one item is already sorted so is ignored, a set *
|
||||
* of two is compared and swapped as necessary. The smaller data *
|
||||
* sets are then repeatedly merged together to eventually form the*
|
||||
* full, sorted, set. *
|
||||
* Since cobol cannot do recursion this module only simulates it *
|
||||
* so is not as fast as a normal recursive version would be. *
|
||||
* Scales very well to larger data sets, its relative complexity *
|
||||
* means it is not suited to sorting smaller data sets: use an *
|
||||
* Insertion sort instead as the Merge sort is a stable sort. *
|
||||
******************************************************************
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
SOURCE-COMPUTER. ICL VME.
|
||||
OBJECT-COMPUTER. ICL VME.
|
||||
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT FA-INPUT-FILE ASSIGN FL01.
|
||||
SELECT FB-OUTPUT-FILE ASSIGN FL02.
|
||||
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
FD FA-INPUT-FILE.
|
||||
01 FA-INPUT-REC.
|
||||
03 FA-DATA PIC 9(6).
|
||||
|
||||
FD FB-OUTPUT-FILE.
|
||||
01 FB-OUTPUT-REC PIC 9(6).
|
||||
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WA-IDENTITY.
|
||||
03 WA-PROGNAME PIC X(10) VALUE "MERGESORT".
|
||||
03 WA-VERSION PIC X(6) VALUE "000001".
|
||||
|
||||
01 WB-TABLE.
|
||||
03 WB-ENTRY PIC 9(8) COMP SYNC OCCURS 100000
|
||||
INDEXED BY WB-IX-1
|
||||
WB-IX-2.
|
||||
|
||||
01 WC-VARS.
|
||||
03 WC-SIZE PIC S9(8) COMP SYNC.
|
||||
03 WC-TEMP PIC S9(8) COMP SYNC.
|
||||
03 WC-START PIC S9(8) COMP SYNC.
|
||||
03 WC-MIDDLE PIC S9(8) COMP SYNC.
|
||||
03 WC-END PIC S9(8) COMP SYNC.
|
||||
|
||||
01 WD-FIRST-HALF.
|
||||
03 WD-FH-MAX PIC S9(8) COMP SYNC.
|
||||
03 WD-ENTRY PIC 9(8) COMP SYNC OCCURS 50000
|
||||
INDEXED BY WD-IX.
|
||||
|
||||
01 WF-CONDITION-FLAGS.
|
||||
03 WF-EOF-FLAG PIC X.
|
||||
88 END-OF-FILE VALUE "Y".
|
||||
03 WF-EMPTY-FILE-FLAG PIC X.
|
||||
88 EMPTY-FILE VALUE "Y".
|
||||
|
||||
01 WS-STACK.
|
||||
* This stack is big enough to sort a list of 1million items.
|
||||
03 WS-STACK-ENTRY OCCURS 20 INDEXED BY WS-STACK-TOP.
|
||||
05 WS-START PIC S9(8) COMP SYNC.
|
||||
05 WS-MIDDLE PIC S9(8) COMP SYNC.
|
||||
05 WS-END PIC S9(8) COMP SYNC.
|
||||
05 WS-FS-FLAG PIC X.
|
||||
88 FIRST-HALF VALUE "F".
|
||||
88 SECOND-HALF VALUE "S".
|
||||
88 WS-ALL VALUE "A".
|
||||
05 WS-IO-FLAG PIC X.
|
||||
88 WS-IN VALUE "I".
|
||||
88 WS-OUT VALUE "O".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
A-MAIN SECTION.
|
||||
A-000.
|
||||
PERFORM B-INITIALISE.
|
||||
|
||||
IF NOT EMPTY-FILE
|
||||
PERFORM C-PROCESS.
|
||||
|
||||
PERFORM D-FINISH.
|
||||
|
||||
A-999.
|
||||
STOP RUN.
|
||||
|
||||
B-INITIALISE SECTION.
|
||||
B-000.
|
||||
DISPLAY "*** " WA-PROGNAME " VERSION "
|
||||
WA-VERSION " STARTING ***".
|
||||
|
||||
MOVE ALL "N" TO WF-CONDITION-FLAGS.
|
||||
OPEN INPUT FA-INPUT-FILE.
|
||||
SET WB-IX-1 TO 0.
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG
|
||||
WF-EMPTY-FILE-FLAG.
|
||||
|
||||
PERFORM BA-READ-INPUT UNTIL END-OF-FILE.
|
||||
|
||||
CLOSE FA-INPUT-FILE.
|
||||
|
||||
SET WC-SIZE TO WB-IX-1.
|
||||
|
||||
B-999.
|
||||
EXIT.
|
||||
|
||||
BA-READ-INPUT SECTION.
|
||||
BA-000.
|
||||
SET WB-IX-1 UP BY 1.
|
||||
MOVE FA-DATA TO WB-ENTRY(WB-IX-1).
|
||||
|
||||
READ FA-INPUT-FILE AT END MOVE "Y" TO WF-EOF-FLAG.
|
||||
|
||||
BA-999.
|
||||
EXIT.
|
||||
|
||||
C-PROCESS SECTION.
|
||||
C-000.
|
||||
DISPLAY "SORT STARTING".
|
||||
|
||||
MOVE 1 TO WS-START(1).
|
||||
MOVE WC-SIZE TO WS-END(1).
|
||||
MOVE "F" TO WS-FS-FLAG(1).
|
||||
MOVE "I" TO WS-IO-FLAG(1).
|
||||
SET WS-STACK-TOP TO 2.
|
||||
|
||||
PERFORM E-MERGE-SORT UNTIL WS-OUT(1).
|
||||
|
||||
DISPLAY "SORT FINISHED".
|
||||
|
||||
C-999.
|
||||
EXIT.
|
||||
|
||||
D-FINISH SECTION.
|
||||
D-000.
|
||||
OPEN OUTPUT FB-OUTPUT-FILE.
|
||||
SET WB-IX-1 TO 1.
|
||||
|
||||
PERFORM DA-WRITE-OUTPUT UNTIL WB-IX-1 > WC-SIZE.
|
||||
|
||||
CLOSE FB-OUTPUT-FILE.
|
||||
|
||||
DISPLAY "*** " WA-PROGNAME " FINISHED ***".
|
||||
|
||||
D-999.
|
||||
EXIT.
|
||||
|
||||
DA-WRITE-OUTPUT SECTION.
|
||||
DA-000.
|
||||
WRITE FB-OUTPUT-REC FROM WB-ENTRY(WB-IX-1).
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
DA-999.
|
||||
EXIT.
|
||||
|
||||
******************************************************************
|
||||
E-MERGE-SORT SECTION.
|
||||
*===================== *
|
||||
* This section controls the simulated recursion. *
|
||||
******************************************************************
|
||||
E-000.
|
||||
IF WS-OUT(WS-STACK-TOP - 1)
|
||||
GO TO E-010.
|
||||
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
|
||||
* First check size of part we are dealing with.
|
||||
IF WC-END - WC-START = 0
|
||||
* Only 1 number in range, so simply set for output, and move on
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1)
|
||||
GO TO E-010.
|
||||
|
||||
IF WC-END - WC-START = 1
|
||||
* 2 numbers, so compare and swap as necessary. Set for output
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1)
|
||||
IF WB-ENTRY(WC-START) > WB-ENTRY(WC-END)
|
||||
MOVE WB-ENTRY(WC-START) TO WC-TEMP
|
||||
MOVE WB-ENTRY(WC-END) TO WB-ENTRY(WC-START)
|
||||
MOVE WC-TEMP TO WB-ENTRY(WC-END)
|
||||
GO TO E-010
|
||||
ELSE
|
||||
GO TO E-010.
|
||||
|
||||
* More than 2, so split and carry on down
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2.
|
||||
|
||||
MOVE WC-START TO WS-START(WS-STACK-TOP).
|
||||
MOVE WC-MIDDLE TO WS-END(WS-STACK-TOP).
|
||||
MOVE "F" TO WS-FS-FLAG(WS-STACK-TOP).
|
||||
MOVE "I" TO WS-IO-FLAG(WS-STACK-TOP).
|
||||
SET WS-STACK-TOP UP BY 1.
|
||||
|
||||
GO TO E-999.
|
||||
|
||||
E-010.
|
||||
SET WS-STACK-TOP DOWN BY 1.
|
||||
|
||||
IF SECOND-HALF(WS-STACK-TOP)
|
||||
GO TO E-020.
|
||||
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2 + 1.
|
||||
|
||||
MOVE WC-MIDDLE TO WS-START(WS-STACK-TOP).
|
||||
MOVE WC-END TO WS-END(WS-STACK-TOP).
|
||||
MOVE "S" TO WS-FS-FLAG(WS-STACK-TOP).
|
||||
MOVE "I" TO WS-IO-FLAG(WS-STACK-TOP).
|
||||
SET WS-STACK-TOP UP BY 1.
|
||||
|
||||
GO TO E-999.
|
||||
|
||||
E-020.
|
||||
MOVE WS-START(WS-STACK-TOP - 1) TO WC-START.
|
||||
MOVE WS-END(WS-STACK-TOP - 1) TO WC-END.
|
||||
COMPUTE WC-MIDDLE = ( WC-START + WC-END ) / 2.
|
||||
PERFORM H-PROCESS-MERGE.
|
||||
MOVE "O" TO WS-IO-FLAG(WS-STACK-TOP - 1).
|
||||
|
||||
E-999.
|
||||
EXIT.
|
||||
|
||||
******************************************************************
|
||||
H-PROCESS-MERGE SECTION.
|
||||
*======================== *
|
||||
* This section identifies which data is to be merged, and then *
|
||||
* merges the two data streams into a single larger data stream. *
|
||||
******************************************************************
|
||||
H-000.
|
||||
INITIALISE WD-FIRST-HALF.
|
||||
COMPUTE WD-FH-MAX = WC-MIDDLE - WC-START + 1.
|
||||
SET WD-IX TO 1.
|
||||
|
||||
PERFORM HA-COPY-OUT VARYING WB-IX-1 FROM WC-START BY 1
|
||||
UNTIL WB-IX-1 > WC-MIDDLE.
|
||||
|
||||
SET WB-IX-1 TO WC-START.
|
||||
SET WB-IX-2 TO WC-MIDDLE.
|
||||
SET WB-IX-2 UP BY 1.
|
||||
SET WD-IX TO 1.
|
||||
|
||||
PERFORM HB-MERGE UNTIL WD-IX > WD-FH-MAX OR WB-IX-2 > WC-END.
|
||||
|
||||
PERFORM HC-COPY-BACK UNTIL WD-IX > WD-FH-MAX.
|
||||
|
||||
H-999.
|
||||
EXIT.
|
||||
|
||||
HA-COPY-OUT SECTION.
|
||||
HA-000.
|
||||
MOVE WB-ENTRY(WB-IX-1) TO WD-ENTRY(WD-IX).
|
||||
SET WD-IX UP BY 1.
|
||||
|
||||
HA-999.
|
||||
EXIT.
|
||||
|
||||
HB-MERGE SECTION.
|
||||
HB-000.
|
||||
IF WB-ENTRY(WB-IX-2) < WD-ENTRY(WD-IX)
|
||||
MOVE WB-ENTRY(WB-IX-2) TO WB-ENTRY(WB-IX-1)
|
||||
SET WB-IX-2 UP BY 1
|
||||
ELSE
|
||||
MOVE WD-ENTRY(WD-IX) TO WB-ENTRY(WB-IX-1)
|
||||
SET WD-IX UP BY 1.
|
||||
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
HB-999.
|
||||
EXIT.
|
||||
|
||||
HC-COPY-BACK SECTION.
|
||||
HC-000.
|
||||
MOVE WD-ENTRY(WD-IX) TO WB-ENTRY(WB-IX-1).
|
||||
SET WD-IX UP BY 1.
|
||||
SET WB-IX-1 UP BY 1.
|
||||
|
||||
HC-999.
|
||||
EXIT.
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
function merge(sequence left, sequence right)
|
||||
sequence result
|
||||
result = {}
|
||||
while length(left) > 0 and length(right) > 0 do
|
||||
if compare(left[1], right[1]) <= 0 then
|
||||
result = append(result, left[1])
|
||||
left = left[2..$]
|
||||
else
|
||||
result = append(result, right[1])
|
||||
right = right[2..$]
|
||||
end if
|
||||
end while
|
||||
return result & left & right
|
||||
end function
|
||||
|
||||
function mergesort(sequence m)
|
||||
sequence left, right
|
||||
integer middle
|
||||
if length(m) <= 1 then
|
||||
return m
|
||||
else
|
||||
middle = floor(length(m)/2)
|
||||
left = mergesort(m[1..middle])
|
||||
right = mergesort(m[middle+1..$])
|
||||
if compare(left[$], right[1]) <= 0 then
|
||||
return left & right
|
||||
elsif compare(right[$], left[1]) <= 0 then
|
||||
return right & left
|
||||
else
|
||||
return merge(left, right)
|
||||
end if
|
||||
end if
|
||||
end function
|
||||
|
||||
constant s = rand(repeat(1000,10))
|
||||
? s
|
||||
? mergesort(s)
|
||||
|
|
@ -1,34 +1,30 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
with javascript_semantics
|
||||
function merge(sequence left, right)
|
||||
sequence result = {}
|
||||
integer ldx = 1, rdx = 1
|
||||
while length(left)>=ldx
|
||||
and length(right)>=rdx do
|
||||
if left[ldx]<=right[rdx] then
|
||||
result = append(result, left[ldx])
|
||||
ldx += 1
|
||||
else
|
||||
result = append(result, right[rdx])
|
||||
rdx += 1
|
||||
end if
|
||||
end while
|
||||
return result & left[ldx..$] & right[rdx..$]
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">merge</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">left</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">right</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]<=</span><span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">result</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">left</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">right</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function mergesort(sequence m)
|
||||
integer middle = floor(length(m)/2)
|
||||
if middle=0 then return m end if
|
||||
sequence left = mergesort(m[1..middle]),
|
||||
right = mergesort(m[middle+1..$])
|
||||
return iff(left[$]<=right[1]?left & right:
|
||||
iff(right[$]<=left[1]?right & left:
|
||||
merge(left, right)))
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mergesort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)<=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">m</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">middle</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">left</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mergesort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">middle</span><span style="color: #0000FF;">]),</span>
|
||||
<span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mergesort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">middle</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$])</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">[$]<=</span><span style="color: #000000;">right</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">left</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">right</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">[$]<=</span><span style="color: #000000;">left</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">right</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">left</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">merge</span><span style="color: #0000FF;">(</span><span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">s</span>
|
||||
<span style="color: #0000FF;">?</span> <span style="color: #000000;">mergesort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
|
||||
<!--
|
||||
constant s = shuffle(tagset(10))
|
||||
? s
|
||||
? mergesort(deep_copy(s))
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
function MergeSort([object[]] $SortInput)
|
||||
{
|
||||
# The base case exits for minimal lists that are sorted by definition
|
||||
if ($SortInput.Length -le 1) {return $SortInput}
|
||||
|
||||
# Divide and conquer
|
||||
[int] $midPoint = $SortInput.Length/2
|
||||
# The @() operators ensure a single result remains typed as an array
|
||||
[object[]] $left = @(MergeSort @($SortInput[0..($midPoint-1)]))
|
||||
[object[]] $right = @(MergeSort @($SortInput[$midPoint..($SortInput.Length-1)]))
|
||||
|
||||
# Merge
|
||||
[object[]] $result = @()
|
||||
while (($left.Length -gt 0) -and ($right.Length -gt 0))
|
||||
{
|
||||
if ($left[0] -lt $right[0])
|
||||
{
|
||||
$result += $left[0]
|
||||
# Use an if/else rather than accessing the array range as $array[1..0]
|
||||
if ($left.Length -gt 1){$left = $left[1..$($left.Length-1)]}
|
||||
else {$left = @()}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result += $right[0]
|
||||
# Without the if/else, $array[1..0] would return the whole array when $array.Length == 1
|
||||
if ($right.Length -gt 1){$right = $right[1..$($right.Length-1)]}
|
||||
else {$right = @()}
|
||||
}
|
||||
}
|
||||
|
||||
# If we get here, either $left or $right is an empty array (or both are empty!). Since the
|
||||
# rest of the unmerged array is already sorted, we can simply string together what we have.
|
||||
# This line outputs the concatenated result. An explicit 'return' statement is not needed.
|
||||
$result + $left + $right
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
include Settings
|
||||
-- 23 Aug 2025
|
||||
include Setting
|
||||
|
||||
say 'MERGE SORT - 4 Mar 2025'
|
||||
say 'MERGE SORT'
|
||||
say version
|
||||
say
|
||||
call Generate
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue