Results 1 to 2 of 2

Thread: BubbleSort Sorting Algorithm

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    BubbleSort Sorting Algorithm

    Here's an algotithm, called the BubbleSort Algorithm.
    It can be used for sorting numbers, strings, or whatever else can be compared using VB's comparative operators.


    VB Code:
    1. Option Explicit
    2.  
    3. Private Function sortInts(arrInt() As Long) As Long()
    4.     Dim i As Long, n As Long, tempN As Long, allOk As Boolean
    5.     For n = 0 To UBound(arrInt)
    6.         allOk = True
    7.         For i = 0 To UBound(arrInt) - 1
    8.             If arrInt(i) > arrInt(i + 1) Then
    9.                 allOk = False
    10.                 tempN = arrInt(i)
    11.                 arrInt(i) = arrInt(i + 1)
    12.                 arrInt(i + 1) = tempN
    13.             End If
    14.         Next
    15.         If allOk Then Exit For
    16.     Next
    17.     sortInts = arrInt
    18. End Function
    19.  
    20. Private Sub Form_Load()
    21.     Dim x() As Long, i As Long
    22.    
    23.     '' redimension array to 100 values
    24.     ''
    25.     ReDim x(99)
    26.    
    27.     '' now fill with random data
    28.     ''
    29.     For i = 0 To 99
    30.         x(i) = CLng(Rnd * 1000)
    31.     Next
    32.    
    33.     '' now sort the array
    34.     ''
    35.     x = sortInts(x)
    36.    
    37.     '' display the data
    38.     For i = 0 To UBound(x)
    39.         Debug.Print x(i)
    40.     Next
    41. End Sub
    Last edited by plenderj; May 22nd, 2002 at 06:17 AM.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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