sorting arrays

A

andrews

Hi,
I have a problem
I have a array as single ar1(N) and a multidimentional array ar2(N,2) as
structurename.
structure structurename
x as integer
y as integer
end structure
ar1(i) has a relation with ar2(i,1).x and with ar2(i,2).y
Now I want quickly sort ar1 so that ar2 is changing to for keeping the
relation f.e. after soting ar1(1000) keeps his relation with
ar2(1000,0),ar2(1000,1) and ar2(1000,2)
I have the entirely code for quicksort but the ar2 give me a problem
For two or three arrays (as integer or single with a relation) I found a
solution with a litle changing in de code of quicksort.
Thanks for any response
 
C

Cor Ligthert[MVP]

Andrews,

Did you already try your sorting your arrays using a dictionary?

Cor
 
B

Branco

andrews wrote:
I have a array as single ar1(N) and a multidimentional array ar2(N,2) as
structurename.
structure structurename
x as integer
y as integer
end structure
ar1(i) has a relation with ar2(i,1).x and with ar2(i,2).y
Now I want quickly sort ar1 so that ar2 is changing to for keeping the
relation f.e. after soting ar1(1000) keeps his relation with
ar2(1000,0),ar2(1000,1) and ar2(1000,2)
I have the entirely code for quicksort but the ar2 give me a problem
For two or three arrays (as integer or single with a relation) I found a
solution with a litle changing in de code of quicksort.
Thanks for any response

There are several approaches that come to mind. The least expensive
coding-wise (although probably highly expensive architecture-wise)
would be to put logic for swapping the related arrays inside the very
quicksort code. To do that you would have to change the quicksort
signature of course. Something around the lines of:

Sub Quicksort(Ar1() As Single, Ar2(,) As StructureName, L As
Integer, R As Integer)
...
End Sub

Now, when it comes the time to swap the elements of the first array,
you swap the elements of the secondary array also. As an example, the
following code will swap the element I of each array with the element
J in the same array:

<example>
Sub Swap(Ar1() As Single, Ar2(,) As StructureName, I As Integer, J
As Integer)

'swaps the elements of the first array
Dim Temp As Single = Ar1(I)
Ar1(I) = Ar1(J)
Ar1(J) = Temp

'swaps the elements of the second array
Dim D As Integer = Ar2.GetUpperbound(1) + 1
Dim DI As Integer = I * D
Dim DJ As Integer = J * D
Dim Temp2(0 To 0, 0 To D - 1) As StructureName
Array.Copy(Ar2, DI, Temp2, 0, D)
Array.Copy(Ar2, DJ, Ar2, DI, D)
Array.Copy(Temp2, 0, Ar2, DJ, D)
End Sub
</example>


Without knowing the implications of changing the data types, I can't
really suggest more general solutions(e.g., if both arrays were some
more structured colletions (a Dictionary maybe) you'd be able to
access both lists by the same key, in such a way that you'd use the
first list's *key* (instead of index) to access the second list's
elements).

HTH.

Regards,

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top