Results 1 to 10 of 10

Adapting Bubblesort to work with Long datatype array

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

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