Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
m (→{{header|F#}}: header cleanup) |
m (→{{header|REXX}}: added DO-END labels, added comments, added whitespace. -- ~~~~) |
||
Line 2,518: | Line 2,518: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program sorts an array using the bubble-sort method. */ |
<lang rexx>/*REXX program sorts an array using the bubble-sort method. */ |
||
call gen@ /*generate array elements. */ |
call gen@ /*generate the array elements. */ |
||
call show@ 'before sort' /*show before array elements*/ |
call show@ 'before sort' /*show the before array elements.*/ |
||
call bubbleSort highItem /*invoke the bubble sort. */ |
call bubbleSort highItem /*invoke the bubble sort. */ |
||
call show@ ' after sort' /*show after array elements*/ |
call show@ ' after sort' /*show the after array elements.*/ |
||
exit /*stick a fork in it, we're done.*/ |
|||
exit |
|||
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/ |
|||
/*─────────────────────────────────────BUBBLESORT subroutine───────*/ |
|||
bubbleSort: procedure expose @.; parse arg n /*n=number of items.*/ |
bubbleSort: procedure expose @.; parse arg n /* n = number of items. */ |
||
/*diminish #items each time.*/ |
/*diminish # items each time. */ |
||
do until done /*sort until it's done. */ |
do until done /*sort until it's done. */ |
||
done=1 /*assume it's done (1=true).*/ |
done=1 /*assume it's done (1 = true). */ |
||
do j=1 for n-1 /*sort M items this time. */ |
do j=1 for n-1 /*sort M items this time. */ |
||
k=j+1 /*point to next item. */ |
k=j+1 /*point to the next item. */ |
||
if @.j>@.k then do /*out of order ? |
if @.j>@.k then do /*is it out of order ? */ |
||
_=@.j /*assign to a temp variable.*/ |
_=@.j /*assign to a temp variable. */ |
||
@.j=@.k /*swap current with next. */ |
@.j=@.k /*swap current with next. */ |
||
@.k=_ /*... and next with _ */ |
@.k=_ /*... and next with _ */ |
||
done=0 /*indicate it's not done. */ |
done=0 /*indicate it's not done. */ |
||
end /* 1=true 0=false */ |
end /* 1=true 0=false */ |
||
end |
end /*j*/ |
||
end /*until done*/ |
|||
end |
|||
return |
return |
||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/ |
|||
/*─────────────────────────────────────GEN@ subroutine─────────────*/ |
|||
gen@: @.= /*assign default |
gen@: @.= /*assign some default values. */ |
||
@.1 ='---letters of the Hebrew alphabet---' |
@.1 ='---letters of the Hebrew alphabet---' ; @.13='kaph [kaf]' |
||
@.2 ='====================================' |
@.2 ='====================================' ; @.14='lamed' |
||
@.3 ='aleph [alef]' |
@.3 ='aleph [alef]' ; @.15='mem' |
||
@.4 ='beth [bet]' |
@.4 ='beth [bet]' ; @.16='nun' |
||
@.5 ='gimel' ; @.17='samekh' |
|||
@.5 ='gimel' |
|||
@.6 ='daleth [dalet]' |
@.6 ='daleth [dalet]' ; @.18='ayin' |
||
@.7 ='he' ; @.19='pe' |
|||
@.7 ='he' |
|||
@.8 ='waw [vav]' |
@.8 ='waw [vav]' ; @.20='sadhe [tsadi]' |
||
@.9 ='zayin' ; @.21='qoph [qof]' |
|||
@.9 ='zayin' |
|||
@.10='heth [het]' |
@.10='heth [het]' ; @.22='resh' |
||
@.11='teth [tet]' |
@.11='teth [tet]' ; @.23='shin' |
||
@.12='yod' ; @.24='taw [tav]' |
|||
@.12='yod' |
|||
@.13='kaph [kaf]' |
|||
@.14='lamed' |
|||
@.15='mem' |
|||
@.16='nun' |
|||
@.17='samekh' |
|||
@.18='ayin' |
|||
@.19='pe' |
|||
@.20='sadhe [tsadi]' |
|||
@.21='qoph [qof]' |
|||
@.22='resh' |
|||
@.23='shin' |
|||
@.24='taw [tav]' |
|||
do highItem=1 while @.highItem\=='' /*find how many entries. */ |
do highItem=1 while @.highItem\=='' /*find how many entries. */ |
||
end |
end /*highitem*/ |
||
highItem=highItem-1 /*adjust highItem slightly. */ |
highItem=highItem-1 /*adjust highItem slightly. */ |
||
return |
return |
||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/ |
|||
/*─────────────────────────────────────SHOW@ subroutine────────────*/ |
|||
show@: widthH=length(highItem) /*maximum width of any line.*/ |
show@: widthH=length(highItem) /*maximum width of any line. */ |
||
do j=1 for highItem |
do j=1 for highItem |
||
say 'element' right(j,widthH) arg(1)':' @.j |
say 'element' right(j,widthH) arg(1)':' @.j |
||
end |
end |
||
say copies(' |
say copies('-',80) /*show a separator line. */ |
||
return</lang> |
return</lang> |
||
'''output''' |
'''output''' |