<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace;font-size:small">The classic (aka simple, aka not super fast) bubble sort is simple 
enough to write in fp in just a few lines.<br>
</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small"><br>I have a routine that sorts a directory listing. Here the array that 
needs to be sorted is dirl[]. It's an array of filenames in no order.
<br>
<br>The starting conditions are:
<br>
<br>* dirl[] is dimmed larger than I think I am likely to need at run-time. 2048 elements, 32 bytes each in my case.<br>
<br>* dirl[] has been loaded with some number of items. It might be anywhere 
from 0 to 2048.
<br>
<br>* n is the number of items that were loaded, 0 to 2048.
<br>
<br>(reasons later)
<br>
<br>Then I gosub bsrt, and after that dirl[] is sorted.
<br>
<br>
<br>Aside from dirl[] and n, it requires a 2nd array that's the same size & 
structure as the one you're sorting and 2 scratch variables for counters 
that are all only used within the gosub, IE you can re-use them for 
other stuff before and after the gosub. Those are tmpl[], i, and r here.
<br>
<br>This is pretty much a direct translation of the optimized bubble sort 
pseudo-code right from the wikipedia entry describing the logic, and 
some BASIC version I found somewhere.
<br>
<br>In MY case, just to show the real example, the array dirl[], and 
count/size n, are filled by:
<br>       ◆ If:
<br>       Then: n=opendir("*",d{"") ;i="1"
<br>cpd    ◆ If: i le n
<br>       Then: dirl[i]=@DIRLIST_FILENAME[i] ;i=i+"1" ;goto cpd
<br>       Then: x=closedir()
<br>
<br>So I have an array that has 2048 elements, and N that says I only care 
about elements 1-to-N. (Well, N can actually be 0 at run-time, that's 
handled too.)
<br>
<br>
<br>And finally the gosub:
<br>
<br>       ◆ If: 'directory list    ;temp copy for bubble sort
<br>       Then: dim dirl[2048](32) ;dim tmpl[2048](32)
<br>bsrt   ◆ If: '***  bubble sort dirl[]  ***
<br>       Then: i="1"
<br>bsi    ◆ If:
<br>       Then: r="1"
<br>bsr    ◆ If: dirl[r] gt dirl[r+"1"]
<br>       Then: tmpl[r]=dirl[r] ;dirl[r]=dirl[r+"1"] ;dirl[r+"1"]=tmpl[r]
<br>       ◆ If: r lt n-i
<br>       Then: r=r+"1" ;goto bsr
<br>       ◆ If: i lt n
<br>       Then: i=i+"1" ;goto bsi
<br>       ◆ If:
<br>       Then: return 'bsrt
<br>
<br>
<br>
<br>Explanation of some of it:
<br>
<br>Why the N?
<br>We can't dim a variable sized array on the fly at run-time, and the size 
of the array matters a lot in a bubble sort, because it has to pass over 
all the items over and over again, only reducing the list size by one 
each time. It takes several seconds to sort even a few hundred items let 
alone 2k. And it's not a linear progression, 200 items takes a lot 
longer than twice as much as 100 items. And whatever size you dim, you 
need a 2nd copy of the same array during the sort. So you dim an array 
that's hopefully larger than you will need at run-time just to allow for 
the worst case, yet as small as you can get away with, and then when you 
fill the array, you keep a count of how many elements you <b class="gmail-moz-txt-star"><span class="gmail-moz-txt-tag">*</span>actually<span class="gmail-moz-txt-tag">*</span></b> 
used in N, and then everywhere else you deal with the array, only count 
up to N.
<br>
<br>The loop counts from 1 to N, yet N=0 is hanlded too
<br>In the case of N=0, the gosub would do one pointless compare/copy of 
dirl[1] and tmpl[1] and immediately return. The data in dirl[1] and 
dirl[2] will have been modified because the first item in the first pass 
gets compared and copied before discovering that 1 is already more than 
N, but nothing should care about that. None of the data anywhere in 
dirl[] matters whenever N is zero. It's either blank, or leftover from 
some previous iteration of a loop or gosub or @key etc. And that one 
compare doesn't cost anything, ie it's not even like a single pass over 
the 2048 items, it's barely more work than doing a test to see if n is 0 
as a special case test before going into the loop in the first place.)
<br>
<br>
<br>
<br><div class="gmail-moz-txt-sig"><span class="gmail-moz-txt-tag">-- <br></span>bkw
<br>
<br>
<br>
<br>On 06/05/2018 11:26 AM, Richard D. Williams via Filepro-list wrote:
<br><blockquote type="cite" style="color:rgb(0,0,0)">Dave,
<br>
<br>I don't know of any function in FP that will sort an array.
<br>
<br>What I do is write the array data out to a temporary file that has an 
index built in the correct order,
<br>clear the array, reach back into the temporary file, and fill the array 
again.
<br>
<br>You can use the tty as a unique identifier for each instance.
<br>
<br>Hope this helps,
<br>
<br>Richard
<br>
<br>
<br>On 6/5/2018 9:28 AM, davidrottkamp via Filepro-list wrote:
<br><blockquote type="cite" style="color:rgb(0,0,0)">Hi
<br>
<br>Is there a way to sort an array made inside filepro
<br>
<br>version
<br>
<br>linux
<br>
<br>
<br></blockquote>
<br>_______________________________________________
<br>Filepro-list mailing list
<br><a class="gmail-moz-txt-link-abbreviated" href="mailto:Filepro-list@lists.celestial.com">Filepro-list@lists.celestial.com</a>
<br>Subscribe/Unsubscribe/Subscription Changes
<br><a class="gmail-moz-txt-link-freetext" href="http://mailman.celestial.com/mailman/listinfo/filepro-list">http://mailman.celestial.com/mailman/listinfo/filepro-list</a>
<br>
<br></blockquote>
</div><br clear="all"></div><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><font face="monospace,monospace">bkw<br></font></div></div>
</div>