Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Displaying each pass of a bubble sort (xp)

    I am populating Range A1:A6 on a worksheet with a unique list of random numbers which I am sorting using the following code:

    Sub Sorting()
    Dim MyArray As Range
    Dim i As Integer
    Dim j As Integer
    Dim y As Integer

    Set MyArray = Sheets("Sheet1").Range("A1:A6")
    Dim Upper As Integer
    Upper = MyArray.Cells.Count
    For i = 1 To Upper - 1
    For j = i + 1 To Upper
    If MyArray.Cells(i).Value > MyArray.Cells(j).Value Then
    y = MyArray.Cells(i).Value
    MyArray.Cells(i).Value = MyArray.Cells(j).Value
    MyArray.Cells(j).Value = y
    End If
    Next j
    Next i
    End Sub

    Everything works beautifully. I would like the results of each pass of the sorting routine to be displayed on the worksheet too, but I am experiencing difficulty (no change there, then). The results can be placed either in rows or columns. I hope I've provided enough details.
    TIA.
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Displaying each pass of a bubble sort (xp)

    I'd change the range to A1:F1
    a) I think rows are easier to read
    [img]/forums/images/smilies/cool.gif[/img] If you want to extend the range, the number of steps might exceed the number of available columns (256).

    Here is a modified version of your macro:

    Sub Sorting()
    Dim MyArray As Range
    Dim i As Integer
    Dim j As Integer
    Dim y As Integer
    Dim k As Long
    Dim Upper As Integer

    Set MyArray = Range("A1:F1")
    Upper = MyArray.Cells.Count
    k = 1
    For i = 1 To Upper - 1
    For j = i + 1 To Upper
    If MyArray.Cells(k, i).Value > MyArray.Cells(k, j).Value Then
    MyArray.Offset(k - 1, 0).Copy Destination:=MyArray.Offset(k, 0)
    k = k + 1
    y = MyArray.Cells(k, i).Value
    MyArray.Cells(k, i).Value = MyArray.Cells(k, j).Value
    MyArray.Cells(k, j).Value = y
    End If
    Next j
    Next i
    End Sub

    See attached workbook.

  3. #3
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Displaying each pass of a bubble sort (xp)

    Thanks so much for that, Hans. It's exactly what I want. As a point of clarification, on the attacment, I can see how row 1 evolves to row 2 after the first pass, but not row 2 to row 3 on the next one. Would you be kind enough to explain this to me. Apologies if I appear more obtuse than normal. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Displaying each pass of a bubble sort (xp)

    The outer loop starts with i = 1, i.e. it takes the first element in the array: 6.
    The inner loop starts with j = i+1 = 2, i.e. it takes the second element in the array: 3
    Since 6 is larger than 3, the array is copied to the next row for display purposes, then 6 and 3 are swapped.
    The first element in the array is now 3.
    The inner loop continues with j = 3, 4, 5 and 6. The 3rd, 4th and 5th elements are all larger than the 1st one, so nothing happens there.
    But the 6th element (value = 2) is smaller than the 1st one (value = 3), so the array is copied again, then 3 and 2 are swapped.
    The inner loop has finished, so the index of the outer loop is incremented to i = 2, etc.

  5. #5
    4 Star Lounger
    Join Date
    Sep 2002
    Location
    Stafford, Staffordshire, England
    Posts
    585
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Displaying each pass of a bubble sort (xp)

    Brilliantly explained, Hans.I've now got it sorted out.
    Many thanks, yet again.
    <font color=blue><font face="Script MT Bold"><big>Rob</big></font face=script></font color=blue>

Posting Permissions

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