Sorting algorithms/Bubble sort: Difference between revisions

Add language: /.ROFF/.
(Add language: /.ROFF/.)
Line 2,580:
END Sort;
END Bubble.</lang>
 
=={{header|N/t/roff}}==
 
This program may output to paper (Postscript/PDF or actual printout) or a line-printer/terminal depending on the device specification.
 
This implementation is not reverse-compatible classical TROFF from Bell Labs, as TROFF then was extremely limited in what it could do. It will work with GNU Troff, though.
 
{{works with|GROFF (GNU Troff)|1.22.2}}
 
<lang N/t/roff>
.ig
Bubble sort algorithm in Troff
==============================
 
:For: Rosetta Code
:Author: Stephanie Björk (Katt)
:Date: December 1, 2017
..
.ig
Array implementation: \(*A
---------------------------
This is an array implementation that takes advantage of Troff's numerical
registers. Registers ``Ax``, where ``x`` is a base-10 Hindu-Arabic numeral and
0 < ``x`` < \(if, are used by array \(*A. The array counter which holds the
number of items in the array is stored in register ``Ac``. This array
implementation is one-indexed (array elements count from 1), though it could be
hardcoded again to become zero-indexed depending on what the programmer favours.
 
\&.. NOTE::
Only integer tokens ``n`` where 0 <= ``n`` < \(if, are allowed in \(*A.
 
.APUSH x
Push item ``x`` into array \(*A.
.AREADLN x..
Push items ``x..`` into array \(*A. This is a shortcut to many successive
calls of the ``.APUSH`` request.
.ASWAP i j
Swap between the ith and jth items of the array \(*A.
.ASORT
Sort all items in array \(*A using the bubble sort algorithm.
..
.nr Ac 0 1 \" Array counter
.
.de APUSH
.nr A\\n+(Ac \\$1
.. \" de APUSH
.
.de AREADLN
.APUSH \\$1
.if \\n(.$>1 \{ \
. shift
. AREADLN \\$*
\} \" if \\n(.$>1
.. \" de AREADLN
.
.de ASWAP
.nr tmp \\n[A\\$1]
.nr A\\$1 \\n[A\\$2]
.nr A\\$2 \\n[tmp]
.rm tmp
.. \" de ASWAP
.
.de ASORT
.nr swapped 1
.nr Ad \\n(Ac+1
.while \\n[swapped] \{ \
. nr swapped 0
. nr Ai 1
. nr Ad -1
. while \\n(Ai<\\n(Ad \{ \
. nr Aj \\n(Ai+1
. if \\n[A\\n(Ai]>\\n[A\\n(Aj] \{ \
. ASWAP \\n(Ai \\n(Aj
. nr swapped 1
\} \" if \\n[A\\n(Ai]>\\n[A\\n(Aj]
. nr Ai +1
\} \" while \\n(Ai<\\n(Ac
\} \" while \\n[swapped]
.. \" de ASORT
.
.ig
Begin Program
-------------
The program's procedural body. Here, we push all our potentially-unsorted
integer tokens sequentially, call a subroutine to sort them, and print all the
sorted items.
..
.AREADLN 12 87 23 77 0 66 45 92 3 0 2 1 9 9 5 4 4 4 \" Our input items to sort.
.ASORT \" Sort all items in the array.
.
.\" Output sorted items
.nr Ai 0 1
.while \n(Ai<\n(Ac \n[A\n+[Ai]]
</lang>
 
=={{header|Nemerle}}==
Anonymous user