URGENT! in Visual Basic, how do I bubblesort?

sandorski

No Lifer
Oct 10, 1999
70,791
6,351
126
Have you done a Bubble Sort before? Do you have an example of a Bubble Sort?

It's been a long time since I did any kind of sort and quite frankly I'd be stumped without taking a refresher course, but sorting methods are basically a collection of commands organized in a certain fashion. Figure out each step, then re-write them in Basic.

If you have no idea what a Bubble sort is, then good luck.

PS: you may want to check out your text books.
 

trmiv

Lifer
Oct 10, 1999
14,670
18
81
I can only help you with an example from my class. If you were using a bubble sort to alphabetize the names Pay, Bob, Willy, Fred, Dave it would look like this. This doesn't format right, but you get the idea.



<< Dim nom(1 to 5) As String

Private Sub cmdSort_Click()
..Dim passNum As Integer, i As Integer, temp As String
....For passNum = 1 to 4
.......For i = 1 to 5 - passNum
...........If nom(i) > nom(i + 1) Then
..............temp = nom(i)
..............nom(i) = nom(i + 1)
..............nom(i+1) = temp
...........End If
.......Next i
....Next passNum
..For i = 1 to 5
.....picNames.print nom(i),
..Next i
End Sub
>>



The periods are just place holders so fusetalk formats correctly.

This is assuming you have an array with the names in it. Basically what happens is this
1)It compars the 1st and 2nd items, then if they are out of order it swaps them
2)It compares the 2nd and 3rd items, then if they are out of order it swaps them.
3)Keep repeating this for the rest of the pairs. The final comparison and swap are with the second-to-last and last elements.