Results 1 to 5 of 5

Thread: [RESOLVED] Bubble Sorting with 2 arrays

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Resolved [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:
    1. Private Sub CommandButton3_Click()
    2. dim a(12) as string, b(12) as single
    3. Dim n As Integer, i As Integer
    4. n = UBound(a)
    5. For j = n To 1 Step -1
    6.    For i = 1 To j - 1
    7.       If b(i) > b(i + 1) Then
    8.          Call swap(b(i), b(i + 1))
    9.          Call swap(a(i), a(i + 1))
    10.       End If
    11.    Next
    12. Next
    13. For i = 1 To n
    14.     Cells(i, 8) = a(i)
    15.     Cells(i, 9) = b(i)
    16. Next i
    17. End Sub

    I don't see any differences, why is that happening? Thanks in advance

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Bubble Sorting with 2 arrays

    n=12. Does it matter? It's in the module.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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).

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    12

    Re: Bubble Sorting with 2 arrays

    Thanks, I change the module to this and it worked
    VB Code:
    1. Sub bsort(a() As String, b() As Single, n As Integer)
    2. Dim i As Integer, j As Integer
    3. For j = n To 1 Step -1
    4.    For i = 1 To j - 1
    5.       If b(i) > b(i + 1) Then
    6.          Call swap(b(i), b(i + 1))
    7.          Call swap(a(i), a(i + 1))
    8.       End If
    9.    Next
    10. Next
    11. 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
  •  



Click Here to Expand Forum to Full Width