Sorting algorithms/Cocktail sort with shifting bounds: Difference between revisions

Line 84:
<br><br>
__TOC__
 
=={{header|360 Assembly}}==
For maximum compatibility, this program uses only the basic instruction set.
The program uses also HLASM structured macros (DO,ENDDO,IF,ELSE,ENDIF) for readability and two ASSIST/360 macros (XDECO,XPRNT) to keep the code as short as possible.
<lang 360asm>* Cocktail sort with shifting bounds 10/05/2020
COCKSHIS CSECT
USING COCKSHIS,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
SAVE (14,12) save previous context
ST R13,4(R15) link backward
ST R15,8(R13) link forward
LR R13,R15 set addressability
* Sort
LA R0,1 1
ST R0,BEGIDX begIdx=LBound(A)
L R0,N n
BCTR R0,0 n-1
ST R0,ENDIDX endIdx=UBound(A)-1
L R1,BEGIDX begIdx
DO WHILE=(C,R1,LE,ENDIDX) while begIdx<=endIdx
MVC NWBEGIDX,ENDIDX nwbegIdx=endIdx
MVC NWENDIDX,BEGIDX nwendIdx=begIdx
L RI,BEGIDX i=begIdx
DO WHILE=(C,RI,LE,ENDIDX) do i=1 to endIdx
LR R1,RI i
SLA R1,2 .
LA R2,A-4(R1) @a(i)
LA R3,A(R1) @a(i+1)
L R4,0(R2) r4=a(i)
L R5,0(R3) r5=a(i+1)
IF CR,R4,GT,R5 THEN if a(i)>a(i+1) then
ST R5,0(R2) a(i)=r5
ST R4,0(R3) a(i+1)=r4
ST RI,NWENDIDX nwendIdx=i
ENDIF , end if
LA RI,1(RI) i=i+1
ENDDO , end do
L R1,NWENDIDX nwendIdx
BCTR R1,0 -1
ST R1,ENDIDX endIdx=nwendIdx-1
LR RI,R1 endIdx
DO WHILE=(C,RI,GE,BEGIDX) do i=endIdx to begIdx by -1
LR R1,RI i
SLA R1,2 .
LA R2,A-4(R1) @a(i)
LA R3,A(R1) @a(i+1)
L R4,0(R2) r4=a(i)
L R5,0(R3) r5=a(i+1)
IF CR,R4,GT,R5 THEN if a(i)>a(i+1) then
ST R5,0(R2) a(i)=r5
ST R4,0(R3) a(i+1)=r4
ST RI,NWBEGIDX nwbegIdx=i
ENDIF , end if
BCTR RI,0 i=i-1
ENDDO , end do
L R1,NWBEGIDX nwbegIdx
LA R1,1(R1) +1
ST R1,BEGIDX begIdx=nwbegIdx+1
ENDDO , endwhile
* Display sorted list
LA R3,PG pgi=0
LA RI,1 i=1
DO WHILE=(C,RI,LE,N) do i=1 to n
LR R1,RI i
SLA R1,2 .
L R2,A-4(R1) a(i)
XDECO R2,XDEC edit a(i)
MVC 0(4,R3),XDEC+8 output a(i)
LA R3,4(R3) pgi=pgi+4
LA RI,1(RI) i=i+1
ENDDO , end do
XPRNT PG,L'PG print buffer
L R13,4(0,R13) restore previous savearea pointer
RETURN (14,12),RC=0 restore registers from calling save
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83'
DC F'782',F'1',F'45',F'82',F'69',F'82',F'104',F'58'
DC F'88',F'112',F'89',F'74'
N DC A((N-A)/L'A) number of items of a
PG DC CL80' ' buffer
XDEC DS CL12 temp for xdeco
BEGIDX DS F begIdx
ENDIDX DS F endIdx
NWBEGIDX DS F nwbegIdx
NWENDIDX DS F nwendIdx
REGEQU opt instruction : 24%
RI EQU 6 i
END COCKSHIS </lang>
{{out}}
<pre>
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
</pre>
 
 
=={{header|ALGOL 60}}==
Line 144 ⟶ 237:
3 6 14 16 19 23 28 33 33 47 61 62 64 67 73 77 78 81 83 92
</pre>
 
 
=={{header|Phix}}==
1,392

edits