|
-
Jan 22nd, 2007, 12:38 PM
#1
Thread Starter
New Member
[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
-
Jan 22nd, 2007, 03:33 PM
#2
Re: Bubble Sorting with 2 arrays
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?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jan 22nd, 2007, 04:00 PM
#3
Thread Starter
New Member
Re: Bubble Sorting with 2 arrays
n=12. Does it matter? It's in the module.
-
Jan 22nd, 2007, 04:22 PM
#4
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).
-
Jan 22nd, 2007, 04:58 PM
#5
Thread Starter
New Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|