Sorting algorithms/Pancake sort: Difference between revisions

m
→‎{{header|REXX}}: restructured most of the REXX program and added DO-end comment labels. -- ~~~~
m (Updated D entry)
m (→‎{{header|REXX}}: restructured most of the REXX program and added DO-end comment labels. -- ~~~~)
Line 1,410:
 
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array using the pancake-sort method algorithm. */
call gen@ /*generate array elements. in the array.*/
call show@ 'before sort' /*show beforethe BEFORE array elements.*/
call pancakeSort highItem /*invoke the pancake sort. */
call show@ ' after sort' /*show the after AFTER array elements.*/
exit
/*──────────────────────────────────PANCAKESORT subroutine──────────────*/
/*─────────────────────────────────────PANCAKESORT subroutine──────*/
pancakeSort: procedure expose @.; parse arg n
 
do n-1
!=@.1; ?=1
do j=2 to n
 
if @.j<=! then iterate
do j=2 to n
!=@.j; ?=j
if @.j<=! then iterate
end /*j*/
!=@.j; ?=j
call flip ?; call flip n
end
n=n-1
 
end /*n-1*/
call flip ?; call flip n
n=n-1
end
 
return
/*──────────────────────────────────FLIP subroutine─────────────────────*/
/*─────────────────────────────────────FLIP subroutine─────────────*/
flip: procedure expose @.; parse arg w
 
do j=1 for (w+1)%2
kmip1=w-j+1; _=@.j; @.j=@.kmip1; @.kmip1=_
end
 
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
/*─────────────────────────────────────GEN@ subroutine─────────────*/
gen@: @.='' /*assign a default value. */
/*Generate some bread primes which are primes of the*/
/*form: (p-3)/2÷2 and 2*p+3 where p is a prime.*/
/*Bread primes are related to sandwich & meat primes.*/
@.1= 2
@.2= 17
@.3= 5
@.4= 29
@.5= 7
@.6= 37
@.7= 13
@.8= 61
@.9= 43
@.10= 181
@.11= 47
@.12= 197
@.13= 67
@.14= 277
@.15= 97
@.16= 397
@.17= 113
@.18= 461
@.19= 137
@.20= 557
@.21= 167
@.22= 677
@.23= 173
@.24= 701
@.25= 797
@.26= 1117
@.27= 307
@.28= 1237
@.29= 1597
@.30= 463
@.31= 1861
@.32= 467
 
@.1= 2 ; @.17= 113
do highItem=1 while @.highItem\=='' /*find how many entries. */
@.2= 17 ; @.18= 461
@.3= 5 ; @.19= 137
@.4= 29 ; @.20= 557
@.5= 7 ; @.21= 167
@.6= 37 ; @.22= 677
@.7= 13 ; @.23= 173
@.8= 61 ; @.24= 701
@.9= 43 ; @.25= 797
@.10= 181 ; @.26= 1117
@.11= 47 ; @.27= 307
@.12= 197 ; @.28= 1237
@.13= 67 ; @.29= 1597
@.14= 277 ; @.30= 463
@.15= 97 ; @.31= 1861
@.16= 397 ; @.32= 467
 
do highItem=1 while @.highItem\=='' /*find how many entries. in array.*/
end
 
highItem=highItem-1 /*adjust the highItem slightly. */
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
/*─────────────────────────────────────SHOW@ subroutine────────────*/
show@: widthH=length(highItem) /*the maximum widhtwidth of any line. */
 
do jk=1 for highItem
say 'element' right(jk,widthH) arg(1)':' @.jk
end /*k*/
 
say copies('-',80) /*show aan eyeball separator line. */
return</lang>
'''output'''