Hi, there seems to something wrong with this code, can't find why it doesn't work like ik should.
Visual Basic code:
'Bubble sort aGetallen
Dim temp As Integer
Dim i As Integer
For AantalLoops As Integer = 1 To UBound(aGetallen)
'number of items
For i = 1 To UBound(aGetallen) - AantalLoops
If (aGetallen(i - 1) > aGetallen(i)) Then
temp = aGetallen(i - 1)
aGetallen(i - 1) = aGetallen(i)
aGetallen(i) = temp
End If
Next
Next AantalLoops
'Display
For x As Integer = 0 To UBound(aGetallen)
TextBox_Sorteer.Text = (TextBox_Sorteer.Text & "Getal" & " " & aGetallen(x) & vbCrLf)
Next x
14-01-2008 at 08:45 AM
|
stickleprojects Level: Moderator Registered: 09-09-2002 Posts: 960
Re: sort numeric array
Hi
It appears to be a partial (incorrect) implementation of a bubble-sort.
When you say it's not working, I assume that the results are partially-sorted, correct?
a more complete implementation is:
Option Explicit
Public Sub SortArray(ar() As Integer)
Dim i As Integer
Dim intNumberOfChanges As Integer
Dim temp As Integer
Do
intNumberOfChanges = 0
'number of items
For i = 1 To UBound(ar)
If (ar(i - 1) > ar(i)) Then
temp = ar(i - 1)
ar(i - 1) = ar(i)
ar(i) = temp
intNumberOfChanges = intNumberOfChanges + 1
End If
Next
Loop While intNumberOfChanges > 0
End Sub
Sub main()
Dim aGetAllen() As Integer
Dim x As Integer
ReDim aGetAllen(10)
Randomize
' Populate some random data
For x = 1 To 10
aGetAllen(x) = Rnd(100) * 100
Next
SortArray aGetAllen
'Display
For x = 0 To UBound(aGetAllen)
Debug.Print aGetAllen(x)
Next x
End Sub
I've coded the sortarray routine by extracting and fixing your code. Have a play.
NOTE: Bubblesort is good if the data is already partially sorted or is a small dataset, you should investigate QuickSort if the data will always be random - there's loads of implementations on google.
Hope this helps,
Kieron
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
14-01-2008 at 09:00 AM
|
stickleprojects Level: Moderator Registered: 09-09-2002 Posts: 960
Re: sort numeric array
OOps.
Just noticed you are using .NET.
look in the help under array.sort
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)