-
Quick Sort
Can someone explain why my lines don't redraw properly in my quicksort tutorial?
Here's the code...
Sub Quicksort(List() As Integer, min As Integer, max As Integer)
' If the list has no more than 1 element, it's sorted.
If min >= max Then Exit Sub
' Pick a dividing item.
i = Int((max - min + 1) * Rnd + min)
med_value = List(i)
' Swap it to the front so we can find it easily.
List(i) = List(min)
' Move the items smaller than this into the left
' half of the list. Move the others into the right.
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While List(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
List(lo) = med_value
Exit Do
End If
' Swap the lo and hi values.
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbBlue
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbBlue
'delay
Sleep (100)
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbBlack
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbBlack
List(lo) = List(hi)
Sleep (100)
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbGreen
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbGreen
' Look up from lo for a value >= med_value.
lo = lo + 1
Do While List(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
List(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbBlue
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbBlue
'delay
Sleep (100)
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbBlack
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbBlack
List(hi) = List(lo)
Sleep (100)
Form1.Line (2400 + lo * 50, 0)-(2400 + lo * 50, List(lo)), vbGreen
Form1.Line (2400 + hi * 50, 0)-(2400 + hi * 50, List(hi)), vbGreen
Loop
' Sort the two sublists
Quicksort List(), min, lo - 1
Quicksort List(), lo + 1, max
End Sub
Thank You
-
Chuck in a Me.Refresh after the drawing code.
-
didn't work
thanx anyways but it didn't work.. this thing is so frustrating
thanx
-
At the end of the loop, just before Loop statement, add:
Me.Enabled = False
DoEvents
Me.Refresh
Me.Enabled = True
Also add the above 4 lines after every "sleep" command.
-
The code that u gave me makes perfect sense but unfortunately it clears the original lines.. if u would like me to send u a copy of the program email me...
[email protected]
thanx