Re: Need someone very familiar with arrays
- From: "rialto" <noname@xxxxxxxxxx>
- Date: Sat, 10 Nov 2007 12:53:59 -0800
"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)
.
- Follow-Ups:
- Re: Need someone very familiar with arrays
- From: Steve Gerrard
- Re: Need someone very familiar with arrays
- From: Larry Serflaten
- Re: Need someone very familiar with arrays
- From: Larry Serflaten
- Re: Need someone very familiar with arrays
- References:
- RE: Need someone very familiar with arrays
- From: KeetClassic
- Re: Need someone very familiar with arrays
- From: Mike Williams
- Re: Need someone very familiar with arrays
- From: rialto
- Re: Need someone very familiar with arrays
- From: rialto
- Re: Need someone very familiar with arrays
- From: Robert Morley
- RE: Need someone very familiar with arrays
- Prev by Date: Re: Is it possible to make the background of a vb6 checkbox transparent?
- Next by Date: Re: Need someone very familiar with arrays
- Previous by thread: Re: Need someone very familiar with arrays
- Next by thread: Re: Need someone very familiar with arrays
- Index(es):
Relevant Pages
|