+ Reply to Thread
Results 1 to 10 of 10

Adapting Bubblesort to work with Long datatype array

Hybrid View

  1. #1
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Adapting Bubblesort to work with Long datatype array

    I've tried about every combination I could think of but no dice...

    This is the original code coming from Microsoft's website and out of the box it works. So I thought, let's try to make it work for me.

    Function BubbleSort(TempArray As Variant)
              Dim Temp As Variant
              Dim i As Integer
              Dim NoExchanges As Integer
    
              ' Loop until no more "exchanges" are made.
              Do
                  NoExchanges = True
    
                  ' Loop through each element in the array.
                  For i = 1 To UBound(TempArray) - 1
    
                      ' If the element is greater than the element
                      ' following it, exchange the two elements.
                      If TempArray(i) > TempArray(i + 1) Then
                          NoExchanges = False
                          Temp = TempArray(i)
                          TempArray(i) = TempArray(i + 1)
                          TempArray(i + 1) = Temp
                      End If
                  Next i
              Loop While Not (NoExchanges)
    
          End Function
    
          Sub BubbleSortMyArray()
              Dim TheArray As Variant
    
              ' Create the array.
              TheArray = Array(15, 8, 11, 7, 33, 4, 46, 19, 20, 27, 43, 25, 36)
    
              ' Sort the Array and display the values in order.
              BubbleSort TheArray
              For i = 1 To UBound(TheArray)
                  MsgBox TheArray(i)
              Next i
          End Sub

    This I what I changed it into to adapt to my needs:
    Function BubbleSort(TempArray As Long)
              Dim Temp As Long
              Dim i As Integer
              Dim NoExchanges As Integer
    
              ' Loop until no more "exchanges" are made.
              Do
                  NoExchanges = True
    
                  ' Loop through each element in the array.
                  For i = 1 To UBound(TempArray) - 1
    
                      ' If the element is greater than the element
                      ' following it, exchange the two elements.
                      If TempArray(i) > TempArray(i + 1) Then
                          NoExchanges = False
                          Temp = TempArray(i)
                          TempArray(i) = TempArray(i + 1)
                          TempArray(i + 1) = Temp
                      End If
                  Next i
              Loop While Not (NoExchanges)
    
          End Function
    
          Sub BubbleSortMyArray()
             Dim TheArray(1 To 10) As Long
    
              
    
              ' Create the array.
    
              TheArray(1) = 100
              TheArray(2) = 200
              TheArray(3) = 40
              
    
              ' Sort the Array and display the values in order.
              BubbleSort TheArray
              For i = 1 To UBound(TheArray)
                  MsgBox TheArray(i)
              Next i
          End Sub
    That get's me the Error 'Byref argument types do not match'.

    So I tried calling it like this:

    BubbleSort (TheArray)
    Which gets me the Error 'Matrix argument must be by ref. '
    Also what goes beyond me is how True can be assigned to the NoExchanges integer instead of a boolean. I do understand how bubblesort works, I just can get it to sort my array. Sorry for being such a noob...
    Last edited by Jeroen1000; 09-23-2009 at 10:16 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Adapting Bubblesort to work with Long datatype array

    As you've rewritten the code, it takes a single variable (a Long), which doesn't leave much to sort. Change it back to a Variant.

    I didn't look past the first line.

    A bubble sort is about the least efficient sort possible, BTW. You wouldn't want to sort more than a few hundred itemes, or even fewer if you're doing it a lot.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    I don't quite follow VBA logic in that case.

    The original code also passes a single variable (one array), but as a Variant.
    When I make the Long array in my code it holds 3 values. The rest is empty.
    The orignal codes also defines an array and puts values into it.

    It looks to me I did the same with longs instead of variants?

    Dim TheArray(1 To 10) As Long
    As I understand, a Variant can hold every datatype. As I only work with 'Long' values I assumed I did not need a Variant array.

    I have written a function before and it had to be declared this way
    Function ErrorHandling(variable As String) As returndatatype
    where as Microsoft's Function does not seem to have a return data type.
    Very confusing to newbies. Still reading up on the matter though.
    Last edited by Jeroen1000; 09-23-2009 at 09:29 AM.

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Adapting Bubblesort to work with Long datatype array

    This works for me

    Function BubbleSort(TempArray() As Long)
              Dim Temp As Long
              Dim i As Integer
              Dim NoExchanges As Integer
    
              ' Loop until no more "exchanges" are made.
              Do
                  NoExchanges = True
    
                  ' Loop through each element in the array.
                  For i = 1 To UBound(TempArray) - 1
    
                      ' If the element is greater than the element
                      ' following it, exchange the two elements.
                      If TempArray(i) > TempArray(i + 1) Then
                          NoExchanges = False
                          Temp = TempArray(i)
                          TempArray(i) = TempArray(i + 1)
                          TempArray(i + 1) = Temp
                      End If
                  Next i
              Loop While Not (NoExchanges)
    
          End Function
    
          Sub BubbleSortMyArray()
             Dim TheArray(1 To 10) As Long
             Dim i As Long
              
    
              ' Create the array.
    
              TheArray(1) = 100
              TheArray(2) = 200
              TheArray(3) = 40
              
    
              ' Sort the Array and display the values in order.
              BubbleSort TheArray
              For i = 1 To UBound(TheArray)
                  MsgBox TheArray(i)
              Next i
          End Sub

  5. #5
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    Thanks Mr. Philips. I've got it running too now. Peculiar how the function is being called now.

    BubbleSort (GatherCodes)
    is not allowed

    I should really go for a tutorial. Too many differences with Java for me.

    As the 'original' array has been altered, is passing by reference the default behavior then?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Adapting Bubblesort to work with Long datatype array

    Arrays are always passed by reference in VBA.

    A Variant can hold any kind of data, including arrays. As the argument to a function, a Variant can receive any type of data passed; but a Variant passed to a function can only be received as a Variant; does that make sense?

    When a VBA function does not have an explicitly declared return type, it is, by default, a Variant, just like declaring a variable without specifying a type.
    Last edited by shg; 09-23-2009 at 10:27 AM.

  7. #7
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Adapting Bubblesort to work with Long datatype array

    Quote Originally Posted by Jeroen1000 View Post
    T Peculiar how the function is being called now.

    BubbleSort (GatherCodes)
    is not allowed
    When you enclose the parameter in brackets, VBA tries to evaualte the expression, and it can't with the array, hence the error.

  8. #8
    Forum Contributor
    Join Date
    09-22-2009
    Location
    Indianapolis, Indiana
    MS-Off Ver
    Office 2003 / Office 2007
    Posts
    114

    Re: Adapting Bubblesort to work with Long datatype array

    Worth pointing out that this is typically a type of option that can be "cheated".

    If the array is in VBA (created by running code etc) then transpose the array directly to the worksheet:

    myarray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    Range("A1:A" & UBound(myarray) + 1).Value = _
    Application.WorksheetFunction.Transpose(myarray)
    Once the values you need to sort are on a sheet, use the built in sort function. This is typically vastly faster than actually attempting to do it with VBA.

  9. #9
    Registered User
    Join Date
    09-21-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    71

    Re: Adapting Bubblesort to work with Long datatype array

    Most interesting explanations. And yes, it does make sense. I learned what what reference and by value was during Java classes

    Also thanks for pointing out what happens with the brackets. I've made much progress incorperation my code and using the 'record macro' cheating for things I don't know.

    It's astounding how much 'crappy code' Excel generates when you record the macro!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1