Results 1 to 40 of 100

Thread: VB6: Sorting algorithms (sort array, sorting arrays)

Threaded View

  1. #20
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: VB6: Sorting algorithms (sort array, sorting arrays)

    At risk of drifting a little off topic, here's what I have found about Mod vs And in VB6.

    Just to be clear And can be used in place of Mod when the divider is a power of 2.

    In the IDE And is much quicker than Mod but when compiled with optimizations on it can appear to run at exactly the same speed. Further tinkering reveals that And is still much quicker but the compiler is clever enough to convert N Mod Power2 into N And (Power2-1) if the divider is hard coded. This can be further shown by comparing N Mod Power2 with N Mod NotPower2 where the power2 version runs at 10x the speed. This only happens when the optimize for speed compile option is selected and where the divider is hard coded.

    It's a real shame VB does not have bit shift operators, they are really useful. However... Again if optimize for speed is selected try comparing N \ Power2 with N \ Not Power2 with a variety of different numbers. N \ Power2 works out 3x faster, so not as fast as a bit shift but something good is going on.

    ______________________________________________
    Edit: Partly in reference to Merri's post below, as safearrays are at the core of this.

    I while ago I started working on a UDT array sorter. It's still not ready (It's a back burner project) but I'm getting re-interested in it again. There's a tricky issue of sorting 8 byte datatypes in UDT's where the element size does not align to multiples of 8 but multiples of 4. All other types are quite easy as they always align thanks to the padding used. I've resolved the 8 byte issue by treating it as two arrays which are sorted separately and then recombined, it still needs a lot of tidying and optimising. At it's core are to be several different versions (one for every data type) of one of the algorithms here, at this stage it's just bubble sort (to keep it simple) but that will change. Not sure which one yet though, it seems like a toss up between Stability and Speed.

    These are the parameters as it stands
    Code:
    Public Function SortArrayX(ByVal ArrayPtr As Long, SortFieldElement As Variant, Optional Descending As Boolean = False) as boolean
    I'm not super happy with this as the return of VarPtrArray has to be passed rather than the array itself, I can't think of any other way to do it. Thankfully it's fairly easy to check that it is a valid array structure and that the passed element does reside in arrays range so it should be safe to use. One of the bonuses of it is that it could in theory sort any one dimensional array (not sub type variant or object) UDT or not, and it should be quicker than a general algorithm that uses a variant to carry the array. Progress is slow for me as this is on the edge of my ability, but I take it this would be something of interest, yes?

    Quote Originally Posted by Merri
    <snip>[*]Create a new identical header that changes to zero base (if I recall correctly you simply change one value!)
    Yes, the array structure stores the lowerbound and the number of elements, the upperbound is calculated.
    Last edited by Milk; May 30th, 2008 at 06:22 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width