Sorting an array
Brian K. White
brian at aljex.com
Fri May 20 01:09:23 PDT 2016
On 5/20/2016 2:37 AM, Brian K. White via Filepro-list wrote:
> Late to the party I know, but, just to show how simple this is, one day
> I had this need myself, and I googled, discovered the bubble sort
> routine along with many others all on a nice wiki article, and indeed
> the bubble sort pseudo-code and BASIC sample code were trivial to
> transpose to filepro. It ended up being a whopping 6 lines of code, and
> 3 of those are just "i=1" and "return"!
>
> This sorts the array named dirl[]
>
> Load up dirl[] with data.
>
> Set N to the number of array elements that actually matter.
> (The arrays are declared with 2048 elements, but I usually only have 10
> or so elements to sort, so it's a big deal to skip sorting 2000 empty
> elements for no reason. In your case you would probably just declare the
> arrays with 20 elements and set N = 20)
>
> And then gosub bsrt
>
> When you return, N elements of dirl[] will be sorted.
>
> It's slooow. That's the nature of bubble sort though. Short simple code
> that doesn't rely on any fancy math or other functions that filePro
> doesn't have. But like you say for 20 elements you won't notice. I'm
> using this in a file selector dialog and it's sorting directory listings
> with a few hundred elements, and it takes an extra maybe 3/4 second vs
> when I comment out the gosub bsrt.
>
> If: 'directory list ; temp copy for bubble sort
> Then: dim dirl[2048](32) ; dim tmpl[2048](32)
> [...]
> Then: n = opendir("*",d{"")
> [...]
> Then: gosub bsrt
> Then: ' dirl[] is now sorted
Fixed a copying error. When I copied this to email I converted a bunch
of long variables like "fsel_i" to just "i" to make the email simpler,
and I missed a couple.
bsrt If: '** bubble sort dirl[] **
Then: i = "1"
bsi If:
Then: r = "1"
bsr If: dirl[r] gt dirl[r+"1"]
Then: tmpl[r]=dirl[r] ;dirl[r]=dirl[r+"1"] ;dirl[r+"1"]=tmpl[r]
If: r lt n-i
Then: r = r + "1" ; goto bsr
If: i lt n
Then: i = i + "1" ; goto bsi
If:
Then: return
--
bkw
More information about the Filepro-list
mailing list