Results 1 to 4 of 4

Thread: Bubble Sort not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    2

    Bubble Sort not working

    Hi, I'm a newbie vb6 user and am wondering why my bubble sorting is not working. Please check and see if what's wrong.

    I have five text boxes with control array 0 to 4 and I can't get them to display in ascending order. The code below seem to have no effect or just duplicate some numbers.


    Code:
    Private Sub Command2_Click()
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim temp As Integer
    
    flag = 1
    Do While flag = 1 And i < 3
    flag = 0
    i = i + 1
    
    For j = 1 To 4 - i
    If txtResult(j + 1) < txtResult(j) Then
    temp = txtResult(j)
    txtResult(j) = txtResult(j + 1)
    txtResult(j + 1) = temp
    flag = 1
    End If
    Next j
    
    Loop
    
    End Sub
    Thanks

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Bubble Sort not working

    The And i < 3 looks like it's not helping the cause.

    You can find a standard bubblesort linked in my signature.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    2

    Re: Bubble Sort not working

    Code:
    Private Sub Command2_Click()
    BubbleSort1 txtResult(i)
    
    End Sub
    I tried calling it, but nothing happened. Do I lack something?

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Bubble Sort not working

    The function is expecting a native array, not an object array, so it won't know what to do with a control array. Store the textbox values to a native array first, sort them, then write the sorted array items back out to the textboxes.

    EDIT: By "native array" I mean an array of longs or strings or whatever.

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