#  sort
#  Copyright (C) 1992  J Dana Eckart
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 1, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CELLULAR; see the file COPYING.  If not, write to the 
#  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# This program demonstrates a  parallel bubble sort.

1 dimensions of 
	which of 0..1
	value of 0..255
end

# Determine which is the other cell with which comparisons (and swapping)
# will be done.
#
if time%2 then
	other := [-1] when cell.which
	      := [1] otherwise
else
	other := [1] when cell.which
	      := [-1] otherwise
end

# Don't sort past the stop mark, which is represented by a 0 value.
#
if cell.value & other.value then
	# Swap the cells in a "bubble" when appropriate.
	#
	if time%2 then
		cell.value := other.value 
				when other.value < cell.value & !cell.which
		           := other.value 
				when cell.value < other.value & cell.which
	else
		cell.value := other.value 
				when other.value < cell.value & cell.which
		           := other.value 
				when cell.value < other.value & !cell.which
	end
end
