Re: Need someone very familiar with arrays

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:utZE3e2IIHA.5468@xxxxxxxxxxxxxxxxxxxxxxx
Given the OP's later statement about not requiring the end list to be sorted, your code would work and be much more efficient, yes. Like KeetClassic, though, you're stuck with the sorting-style idea that the values need to be "swapped"; they don't. You can take non-zero values off the end and just keep scanning backwards through the string, then use ReDim to lob the moved values off the end all in one shot. Also, it would *probably* be faster to include "start < finish" checks in each loop, rather than have your start and finish loops scan through the entire array once the array is cleared out. And finally, it has been established in another thread that Do While...Loop is significantly faster than While...Wend. So here's what we would have with all of the above taken into account...

Dim start As Integer
Dim finish As Integer
Dim zero_count As Integer

zero_count = 0
start = LBound(a)
finish = UBound(a)
Do
Do While a(start) <> 0 And start < finish
start = start + 1
Loop
Do While a(finish) = 0 And start < finish
finish = finish - 1
Loop
If start < finish Then
a(start) = a(finish)
zero_count = zero_count + 1
End If
Loop Until start >= finish
ReDim Preserve a(finish - 1)


Rob

You got a bug. If non-zero elements are moved, then you must assign a zero to where those elements were moved from or the interior start loop will not know when to stop. It would in effect move until it reached the last non zero item in the list and then write that value repeatedly whenever a zero was encountered. I do agree that a swap is not necessary and have corrected that as well as replacing the While-Wend starements. Because the loop "does" know where to stop, you don't need the start < finish checks in the interior loops. I also believe the Redim Perserve a(finish) is correct, not a(finish - 1). This code was tested on a data sample with a limited zero values as might be expected in the random data generation the OP used. I think it would fail if the array contained all zero values or if there are fewer than two elements in the array.

start = LBound(a)
finish = UBound(a)
Do
Do While a(start) <> 0
start = start + 1
Loop
Do While a(finish) = 0
finish = finish - 1
Loop
If start < finish Then
a(start) = a(finish)
a(finish) = 0 ' this line IS needed
End If
Loop Until Not start < finish
ReDim Preserve a(finish)

.



Relevant Pages

  • Re: Strange result from coding error?
    ... You happened to choose zero. ... LBOUNDsuch that loops through the array don't store ... also had the undefined state then I would agree. ... That test could easily be inside a deeply nested loop, ...
    (comp.lang.fortran)
  • Re: should every thing be zero indexed?
    ... Other families of languages count from 1. ... its number is zero: I always start counting at zero". ... > as a fundamental concept in both the definition of array indices and ... > for I in foo'range loop ...
    (comp.programming)
  • Re: float point arithmetic a-a != 0.0
    ... Let's say we have a positive double array x, ... use a for loop I find out the minimal value of in x, ... but not zero. ... though that exact example is unlikely because the optimiser ...
    (comp.lang.c)
  • Re: creating an array on the fly
    ... having some trouble building the array. ... if the checkbox is true, it returns the value of the corresponding text box. ... Each time in your loop you need the statement "Redim Preserve" prior to ...
    (microsoft.public.excel.programming)
  • Re: question about data movement
    ... positions were previously initialized to zero, ... The obvious answer is to reverse the "sense" of your array so rather ... Messing with your loop counter inside the loop body is one. ... int current = new_value; ...
    (rec.games.roguelike.development)