It is currently Wed Apr 26, 2017 8:15 pm



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Sorting multiple arrays
Unread postPosted: Tue Feb 09, 2016 10:44 am 
Offline

Joined: Mon Feb 08, 2016 9:07 am
Posts: 106
A small snippet of code may be helpful to sort more than one array simultaneously, i.e pairs of related
x,y data.
The time needed for sorting two arrays x[1000] and y[1000] is about 1.4 seconds on my device.
The code can easily be extended for more than two arrays.

Regards,
nobs

Code:
REM Multiple array sort

randomize 0
anz=10
DIM x[anz], y[anz]
! Generate some data
FOR i=1 TO anz
x[i]=10*RND() : y[i]=1/x[i]
print x[i],y[i]
NEXT i

! Sort
t1=TIME()
FOR i=1 TO anz-1
ARRAY.MIN mn,x[i,anz-i+1]
ARRAY.SEARCH x[i,anz],mn,pos
SWAP x[i],x[pos+i-1]
SWAP y[i],y[pos+i-1]
NEXT i
t2=TIME()

FOR i=1 TO anz
print x[i],y[i]
NEXT i

PRINT (t2-t1)/1000;" sec."


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Tue Feb 09, 2016 11:04 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5485
Location: Paris, France
Interesting nobs!
I did a similar thing in Bookworm I remember, made a user function out of it (from memory SortDescend_TagAlong(x[], y[]) or similar name)

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Tue Feb 09, 2016 12:14 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 805
With a slight modification you could get another noticable increase of speed: by useing ARRAY.COPY and therefore reducing the amount of instructions inside loop:

Code:
ARRAY.COPY x[] , xx[]
ARRAY.COPY y[] , yy[]
ARRAY.SORT x[]
FOR i=1 TO anz
  ARRAY.SEARCH xx[], x[i] ,pos
  y[i] = yy[pos]
NEXT



Another little point:
Code:
randomize 0

throws no error ..but has no effect. You would have to use
Code:
dummy = randomize (x)

with x<>0
(x=0 has also no effect, because this is the standard (pseudo-random) seed)

regards, brochi


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Tue Feb 09, 2016 1:19 pm 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2795
Location: Colorado, U.S.
If you just put a variable name on a line by itself, BASIC! creates that variable and initializes it to 0 or "", depending on type.

Also, when BASIC! is executing it ignores all spaces (outside of quotes). "go to x y z" is the same as "goto xyz". Or "gotoxyz".

Last piece of the puzzle: RANDOMIZE is a function, so you have to say something like "x = RANDOMIZE(0)". (Always returns 0, which makes no sense, but that's another story.)

So the statement
RANDOMIZE 0
creates a variable named "RANDOMIZE0" and initializes it to 0.

This "bare variable is a complete statement" rule is quite recent. If your experience with "RANDOMIZE 0" makes you think it's a bad idea, you are not alone. Read this topic. Although I did not say so there, I did change the behavior. In v01.90, your "RANDOMIZE 0" will generate a syntax error.

- Marc


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Tue Feb 09, 2016 6:13 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 421
Thanks! I needed this..


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Wed Feb 10, 2016 1:09 am 
Offline

Joined: Mon Feb 08, 2016 9:07 am
Posts: 106
Thanks to everybody for your very useful remarks.
Great code, brochi. It only takes half of the time compared to my code!


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Wed Feb 10, 2016 5:06 pm 
Offline

Joined: Wed Apr 15, 2015 3:57 am
Posts: 421
I don't think brochi's code works when there are duplicate x[] values...copies the first occurrence twice?


Top
 Profile  
 
 Post subject: Re: Sorting multiple arrays
Unread postPosted: Wed Feb 10, 2016 7:16 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 805
Good objection, thanks!

I have to admit, that i did not tested very deep, only with the given example-(random-) values....
Below a possible fix (...hopefully). Speed benefit is melting...but still noticable.

regards, brochi

Code:
ARRAY.COPY x[] , xx[]
ARRAY.COPY y[] , yy[]
ARRAY.SORT x[]
DO: dummy=RND(): ARRAY.SEARCH x[], dummy ,pos : UNTIL !pos
FOR i=1 TO anz
  ARRAY.SEARCH xx[], x[i] , pos
  y[i]      = yy[pos]
  xx[pos]   = dummy
NEXT


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred