Results 1 to 1 of 1

Thread: VB6 - Bubble Sort "paper trail"

  1. #1

    Thread Starter
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    VB6 - Bubble Sort "paper trail"

    This bit of code will display a "paper trail" of (ahem) sorts,
    showing the processing of a basic bubble sort algo.

    An image of this "paper trail" is attached (below)

    On your form you'll need:
    • CommandButton named cbSort
    • MSHFlexGrid named FG1

    Code:
    Private Sub cbSORT_Click()
        '
        txt0 = "1,11,6,3,8,5,9,10"  ' orignal string
        txt1 = ""                   ' sorted string
        '
        xtr0 = Split(txt0, ",")     ' "temp" array for split (values are strings)
        nn = UBound(xtr0)
        ' 0. populate "real" array with values
        Dim aXTR()
        ReDim aXTR(nn)
        For ii = 0 To nn
            aXTR(ii) = Val(xtr0(ii))
        Next ii
        ns = 0
        ' 1. do bubble sort
        For ii = nn To 0 Step -1
            didswap = 0
            For jj = 0 To ii - 1
                If aXTR(jj) > aXTR(jj + 1) Then
                    swap = aXTR(jj)
                    aXTR(jj) = aXTR(jj + 1)
                    aXTR(jj + 1) = swap
                    didswap = 1
                    ns = ns + 1
                End If
            Next jj
            If didswap = 0 Then
                Exit For
            End If
        Next ii
        ' 2. create "sorted" txt string
        For ii = 0 To nn
            txt1 = txt1 + Trim(aXTR(ii)) + IIf(ii = nn, "", ",")
        Next ii
        ' 3. set up FG for display purposes
        With FG1
            .Top = 5000
            .Left = 5000
            .Clear
            .Height = (ns + 1) * 240
            .Visible = True
            .Cols = nn + 2
            .Rows = ns + 1
            .FixedCols = 1
            .FixedRows = 1
            tw = 0
            cw = 450
            For ii = 0 To nn + 1
                .ColWidth(ii) = cw
                tw = tw + cw
            Next ii
            .Width = tw
            .TextMatrix(0, 0) = "Pass"
            ' pass 0
            .Row = 0
            For cc = 1 To nn + 1
                .TextMatrix(0, cc) = xtr0(cc - 1)
                .Col = cc
                .CellAlignment = 3
            Next cc
        End With
        ' 4. redo for display purposes
        For ii = 0 To nn
            aXTR(ii) = Val(xtr0(ii))
        Next ii
        ns = 0
        For ii = nn To 0 Step -1
            didswap = 0
            For jj = 0 To ii - 1
                If aXTR(jj) > aXTR(jj + 1) Then
                    swap = aXTR(jj)
                    aXTR(jj) = aXTR(jj + 1)
                    aXTR(jj + 1) = swap
                    didswap = 1
                    ns = ns + 1
                    ' display "paper trail" of what we just did
                    With FG1
                        .Row = ns
                        .Col = 0
                        .Text = ns
                        .CellAlignment = 3
                        For cc = 1 To nn + 1
                            .Col = cc
                            .Text = aXTR(cc - 1)
                            .CellAlignment = 3
                            If cc = jj + 1 _
                            Or cc = jj + 2 Then
                                .CellBackColor = RGB(200, 255, 255)
                            End If
                        Next cc
                    End With
                End If
            Next jj
            If didswap = 0 Then
                Exit For
            End If
        Next ii
    End Sub
    ... where
    • txt0 = "1,11,6,3,8,5,9,10"
    • txt1 = "1,3,5,6,8,9,10,11"


    Credit to Ellis Dee

    In the image below,
    • top row shows original sequence
    • each row represents a Pass, and shows the current "swap event"
    • the 2 values that were swapped are shown with a cyan backcolor
    • bottom row shows fully sorted sequence

    Spoo

    .
    Attached Images Attached Images  

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