Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #19295

Re: how to osrt 5 numbers

From "Charles Childers" <crc@retroforth.org>
Newsgroups comp.lang.forth
Subject Re: how to osrt 5 numbers
Date 2013-01-30 15:35 -0500
Organization ForthWorks
Message-ID <op.wrqrhhr16ef20b@denney-2466a6df> (permalink)
References <b58af4d3-e05e-43bf-b867-fcf763655bf3@googlegroups.com>

Show all headers | View raw


On Tue, 29 Jan 2013 17:13:46 -0500, the_gavino_himself
<visphatesjava@gmail.com> wrote:

> 5 3 33 22 333  smallest to largest
>
> I have not mastered shallow stack!
>
> How is this done?

Implementations of various sorting algorithms can be found on
rosettacode.org. For a few examples, see:

http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Forth
http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Forth


A quick, dirty, and inefficient method (in parable):

[ over over < [ swap ] ifTrue ] 'minimize' define

[ ] 'smallest-to-largest' define
[ minimize stack.depth #2 > [ [ smallest-to-largest ] dip ] ifTrue ]  
'smallest-to-largest' define

[ stack.depth [ smallest-to-largest ] timesRepeat ] 'reduce' define

#5 #3 #33 #22 #333 reduce


I'm pretty sure that a gforth compatible translation (not tested) would  
look like:

: minimize 2dup > if swap then ;
: smallest-to-largest minimize depth 2 > if >r recurse r> then ;
: reduce depth 0 do smallest-to-largest loop ;


Realistically though, its usually better to construct a buffer or array in  
memory rather than sorting with the values on the stack.

-- crc

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to osrt 5 numbers the_gavino_himself <visphatesjava@gmail.com> - 2013-01-29 14:13 -0800
  Re: how to osrt 5 numbers "Elizabeth D. Rather" <erather@forth.com> - 2013-01-29 12:45 -1000
  Re: how to osrt 5 numbers Brad Eckert <hwfwguy@gmail.com> - 2013-01-30 11:56 -0800
    Re: how to osrt 5 numbers "Ed" <invalid@nospam.com> - 2013-01-31 10:41 +1100
  Re: how to osrt 5 numbers "Charles Childers" <crc@retroforth.org> - 2013-01-30 15:35 -0500
  Re: how to osrt 5 numbers Hugh Aguilar <hughaguilar96@yahoo.com> - 2013-01-30 18:49 -0800
  Re: how to osrt 5 numbers Zbiggy <zbigniew2011REMOVE@gmail.REMOVE.com> - 2013-02-01 14:53 +0100

csiph-web