Jbase Sort Numbers

1,004 views
Skip to first unread message

master

unread,
Jan 30, 2012, 5:40:06 AM1/30/12
to jBASE
Hi,

Is there a function to sort numerics (float and ints) in jbase?
Sample this:

TEST = -1 : @FM : 4 :@FM: -399 : @FM : 19
TEST2 = SORT(TEST)

This gives
TEST2 : -1^-399^19^4

The function treats the values as string and therefore the result is,
as per the expected outcome, wrong.

Daniel Klein

unread,
Jan 30, 2012, 8:48:54 AM1/30/12
to jb...@googlegroups.com
The SORT() function can only do left-justified sorts and there is nothing intrinsic in jBASE that will do this for you.

The actual solution depends on how large the list of numbers, that needs to be sorted, is.

I'm sure other members will chime in with better/faster solutions but one solution is to use LOCATE, e.g.

0001     TEST = -1 : @FM : 4 :@FM: -399 : @FM : 19
0002     N = DCOUNT(TEST,@FM)
0003     TEST1 = ''
0004     FOR X = 1 TO N
0005         NUMBER = TEST<X>
0006         LOCATE NUMBER IN TEST1 BY "AN" SETTING POS ELSE NULL
0007         INS NUMBER BEFORE TEST1<POS>
0008     NEXT X
0009     CRT TEST
0010     CRT TEST1

Another solution would be to use a binary search and insertion (ref. Knuth's TAOCP Vol 3, Sorting and Searching), e.g.

0001     unsortedNumbers = -1 : @FM : 4 :@FM: -399 : @FM : 19
0002     sortedNumbers = ''
0003     n = DCOUNT(unsortedNumbers, @FM)
0004     FOR x = 1 TO n
0005         number = unsortedNumbers<x>
0006         found = 0
0007         lowerBound = 1
0008         upperBound = DCOUNT(sortedNumbers, @FM)
0009         LOOP UNTIL lowerBound > upperBound OR found DO
0010             midPoint = INT((lowerBound + upperBound) / 2)
0011             midValue = sortedNumbers<1,midPoint>
0012             IF number = midValue THEN found = midPoint ELSE
0013                 IF number < midValue THEN upperBound = midPoint - 1 ELSE lowerBound = midPoint + 1
0014             END
0015         REPEAT
0016         IF NOT(found) THEN found = lowerBound
0017         INS number BEFORE sortedNumbers<found>
0018     NEXT x
0019     CRT unsortedNumbers
0020     CRT sortedNumbers

I hear-tell that Quicksort would be another option.

I suspect the binary search method will be faster but ymmv depending on how many numbers are in the original list.

But what do I know ;-)

Dan

--
IMPORTANT: T24/Globus posts are no longer accepted on this forum.

To post, send email to jB...@googlegroups.com
To unsubscribe, send email to jBASE-un...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en

VK

unread,
Jan 30, 2012, 8:20:28 AM1/30/12
to jBASE
Hi,
use LOCATE...INS to another array, like:

* create array
V.ARR = ’’
FOR V.I = 1 TO 1000
V.ARR := @FM : RND(1000)
NEXT V.I
DEL V.ARR<1>
* sort it
V.SORTED = ’’
FOR V.I = 1 TO 1000
V.IN = V.ARR<V.I>
LOCATE V.IN IN V.SORTED<1> BY ’AN’ SETTING V.INS.POSN ELSE NULL
INS V.IN BEFORE V.SORTED<V.INS.POSN>
NEXT V.I


VK

master

unread,
Jan 30, 2012, 10:46:35 AM1/30/12
to jBASE
Thanx so much Dan,

Its actually a relatively small list (potentially a max of 30 values
to represent daily amounts in a month) so am going with the LOCATE
method.

Thank you again
Reply all
Reply to author
Forward
0 new messages