[RESOLVED] Bubble Sorting with 2 arrays
Hi all, this problem has been giving me headaches all day. I have a string array called a(12) and one single with values called b(12). I want to sort the b(12) from low to high but at the same time sort the a(12) when needed to keep the matches (ex a(4) with b(4). I want to use the bubble sort routine as module.
This is what I have in mind which doesn't work
Code:
Sub bsort(a() As String, b() As Single)
Dim n As Integer
Dim i As Integer, j As Integer
For j = n To 1 Step -1
For i = 1 To j - 1
If b(i) > b(i + 1) Then
Call swap(b(i), b(i + 1))
Call swap(a(i), a(i + 1))
End If
Next
Next
End Sub
Instead if I use this one in command button of a user form it works
VB Code:
Private Sub CommandButton3_Click()
dim a(12) as string, b(12) as single
Dim n As Integer, i As Integer
n = UBound(a)
For j = n To 1 Step -1
For i = 1 To j - 1
If b(i) > b(i + 1) Then
Call swap(b(i), b(i + 1))
Call swap(a(i), a(i + 1))
End If
Next
Next
For i = 1 To n
Cells(i, 8) = a(i)
Cells(i, 9) = b(i)
Next i
End Sub
I don't see any differences, why is that happening? Thanks in advance :cool:
Re: Bubble Sorting with 2 arrays
Quote:
Sub bsort(a() As String, b() As Single)
Dim n As Integer
Dim i As Integer, j As Integer
For j = n To 1 Step -1
For i = 1 To j - 1
If b(i) > b(i + 1) Then
Call swap(b(i), b(i + 1))
Call swap(a(i), a(i + 1))
End If
Next
Next
End Sub
what is the value of 'n' above?
Re: Bubble Sorting with 2 arrays
n=12. Does it matter? It's in the module.
Re: Bubble Sorting with 2 arrays
The value is not 12, it is 0. ;)
You can check this by setting a breakpoint on the "For" line (by clicking the line, then pressing F9) and running the code. When it gets there, the code will stop and you can hover over the variable name to see the value.
This is because you have declared the variable, but not given it a value (as you did in the other code).
Re: Bubble Sorting with 2 arrays
Thanks, I change the module to this and it worked :)
VB Code:
Sub bsort(a() As String, b() As Single, n As Integer)
Dim i As Integer, j As Integer
For j = n To 1 Step -1
For i = 1 To j - 1
If b(i) > b(i + 1) Then
Call swap(b(i), b(i + 1))
Call swap(a(i), a(i + 1))
End If
Next
Next
End Sub