Sorting routine

This is a discussion on Sorting routine within the Forth forums in Programming Languages category; I am being lazy, thought I would ask if you could point me in the direction of a sorting routine. I simply want to sort the elements in an array. I have done it before just didn't want to start again from scratch. Yes I did search the archives and nothing turned up. Frank...

Go Back   Application Development Forum > Programming Languages > Forth

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-29-2008, 04:03 PM
Frank
Guest
 
Default Sorting routine

I am being lazy, thought I would ask if you could point me in the
direction of a sorting routine. I simply want to sort the elements in
an array. I have done it before just didn't want to start again from
scratch. Yes I did search the archives and nothing turned up.

Frank
Reply With Quote
  #2  
Old 08-29-2008, 05:23 PM
roger.levy@gmail.com
Guest
 
Default Re: Sorting routine

On Aug 29, 4:03*pm, Frank <fjru...@yahoo.com> wrote:
> I am being lazy, thought I would ask if you could point me in the
> direction of a sorting routine. *I simply want to sort the elements in
> an array. *I have done it before just didn't want to start again from
> scratch. Yes I did search the archives and nothing turned up.
>
> Frank


I used this: http://en.literateprograms.org/Quicksort_(Forth) as a
starting point for a Z-sorting routine in my 3D scenegraph engine
Reply With Quote
  #3  
Old 08-29-2008, 05:26 PM
John Passaniti
Guest
 
Default Re: Sorting routine

Frank wrote:
> I am being lazy, thought I would ask if you could point me in the
> direction of a sorting routine. I simply want to sort the elements in
> an array. I have done it before just didn't want to start again from
> scratch. Yes I did search the archives and nothing turned up.


What kind of data is in each element of the array?

What kind of comparison is needed to know the order?

How many elements in the array?

How unsorted is the array (completely random, only a few entries out of
order, unknown)?

How often do you need to sort (just once when you start, whenever you
get new data, periodically at some rate)?

Do you know why I'm asking you these questions?
Reply With Quote
  #4  
Old 08-29-2008, 05:38 PM
C. G. Montgomery
Guest
 
Default Re: Sorting routine

Frank fjrusso@yahoo.com wrote:

> I am being lazy, thought I would ask if you could point me in the
> direction of a sorting routine. I simply want to sort the elements in
> an array. I have done it before just didn't want to start again from
> scratch. Yes I did search the archives and nothing turned up.
>
> Frank


Algorithm 15 in the Forth Scientific Library is a shell sort for floating
point arrays, which could easily be adapted to arrays of other types.
http://www.taygeta.com/fsl/library/shellsrt.seq

If a shell sort is suitable for your problem.

regards cgm
Reply With Quote
  #5  
Old 08-29-2008, 05:42 PM
Frank
Guest
 
Default Re: Sorting routine

Thank you John for reminding me...Yes I know why you are asking. I am
a scientific-engineer and the more info I have the better I can answer
a question.

The data is numeric, completely random. It needs to be sorted any time
the list grows. The list starts at ~ 260 and can grow to well no
limit. The array is dynamic and increases in size as it needs. At the
moment it has 350 of 512 elements occupied. Imagine the list is your
vocabulary in a hash form it grows as you do.

Frank


On Aug 29, 5:26*pm, John Passaniti <n...@JapanIsShinto.com> wrote:
> Frank wrote:
> > I am being lazy, thought I would ask if you could point me in the
> > direction of a sorting routine. *I simply want to sort the elements in
> > an array. *I have done it before just didn't want to start again from
> > scratch. Yes I did search the archives and nothing turned up.

>
> What kind of data is in each element of the array?
>
> What kind of comparison is needed to know the order?
>
> How many elements in the array?
>
> How unsorted is the array (completely random, only a few entries out of
> order, unknown)?
>
> How often do you need to sort (just once when you start, whenever you
> get new data, periodically at some rate)?
>
> Do you know why I'm asking you these questions?


Reply With Quote
  #6  
Old 08-29-2008, 05:45 PM
Frank
Guest
 
Default Re: Sorting routine

Thanks for the link but it failed:

Quicksort (Forth
From LiteratePrograms
Jump to: navigation, search
There is currently no text in this page

Got another?

Frank

On Aug 29, 5:23*pm, "roger.l...@gmail.com" <roger.l...@gmail.com>
wrote:
> On Aug 29, 4:03*pm, Frank <fjru...@yahoo.com> wrote:
>
> > I am being lazy, thought I would ask if you could point me in the
> > direction of a sorting routine. *I simply want to sort the elements in
> > an array. *I have done it before just didn't want to start again from
> > scratch. Yes I did search the archives and nothing turned up.

>
> > Frank

>
> I used this:http://en.literateprograms.org/Quicksort_(Forth) *as a
> starting point for a Z-sorting routine in my 3D scenegraph engine


Reply With Quote
  #7  
Old 08-29-2008, 06:06 PM
Marcel Hendrix
Guest
 
Default Re: Sorting routine

Frank <fjrusso@yahoo.com> writes Re: Sorting routine

> Thanks for the link but it failed:


> Quicksort (Forth
> From LiteratePrograms
> Jump to: navigation, search
> There is currently no text in this page


> Got another?


Quartus' page:

http://www.google.com/custom?num=100...%3A1qayaf_i0fw

The only one I know by heart:

: bubble ( -- )
#elements
1 DO A-list #elements I - CELLS
BOUNDS DO I 2@ > IF I 2@ I D! ENDIF CELL +LOOP
LOOP ;

But there is really too much to read about this on-line, even in Forth.

-marcel


Reply With Quote
  #8  
Old 08-29-2008, 06:20 PM
Ian Osgood
Guest
 
Default Re: Sorting routine

On Aug 29, 3:06*pm, Thomas Pornin <por...@bolet.org> wrote:
> According to Frank *<fjru...@yahoo.com>:
>
> > I am being lazy, thought I would ask if you could point me in the
> > direction of a sorting routine. *I simply want to sort the elements in
> > an array. *I have done it before just didn't want to start again from
> > scratch. Yes I did search the archives and nothing turned up.

>
> A good question might be: why isn't a sort word defined in
> ANS Forth ? Even C has qsort().
>
> * * * * --Thomas Pornin


For exactly the reasons that John Passaniti asked all those questions.
Each different answer leads you to a different sorting algorithm.

Also, some other sorting algorithm implementations can be found on
Rosetta Code in the "Sorting Algorithms" category.

http://www.rosettacode.org/wiki/Cate...ing_Algorithms

http://www.rosettacode.org/wiki/Quicksort#Forth

http://www.rosettacode.org/wiki/Insertion_sort#Forth

Review and additional Forth implementations welcome.

Ian
Reply With Quote
  #9  
Old 08-29-2008, 06:29 PM
Ian Osgood
Guest
 
Default Re: Sorting routine

On Aug 29, 2:42*pm, Frank <fjru...@yahoo.com> wrote:
> Thank you John for reminding me...Yes I know why you are asking. *I am
> a scientific-engineer and the more info I have the better I can answer
> a question.
>
> The data is numeric, completely random. It needs to be sorted any time
> the list grows. The list starts at ~ 260 and can grow to well no
> limit. The array is dynamic and increases in size as it needs. At the
> moment it has 350 of 512 elements occupied. Imagine the list is your
> vocabulary in a hash form it grows as you do.
>
> Frank


Sounds like a binary search plus insertion should be good enough to
maintain a sorted array:

http://wiki.forthfreak.net/index.cgi...hInsertionSort

http://www.rosettacode.org/wiki/Binary_search#Forth

This is kind of a toy implementation. Use with a growable list of
strings (via ALLOCATE-FREE) is found in this application:

http://wiki.forthfreak.net/index.cgi?BoggleGame

Ian
Reply With Quote
  #10  
Old 08-29-2008, 07:14 PM
Frank
Guest
 
Default Re: Sorting routine

On Aug 29, 4:03*pm, Frank <fjru...@yahoo.com> wrote:
> I am being lazy, thought I would ask if you could point me in the
> direction of a sorting routine. *I simply want to sort the elements in
> an array. *I have done it before just didn't want to start again from
> scratch. Yes I did search the archives and nothing turned up.
>
> Frank


Thank you one and all, you pointed me in a good direction.

Frank
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:57 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.