borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (numeric keypad) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : sort numeric array
Poster Message
Fatboy58
Level: Trainee

Registered: 14-01-2008
Posts: 2

icon sort numeric array

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
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 960
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 960
icon 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)

14-01-2008 at 09:01 AM
View Profile Send Email to User Show All Posts | Quote Reply
Fatboy58
Level: Trainee

Registered: 14-01-2008
Posts: 2
icon Re: sort numeric array

Ok thanks ,

i have changed  : For i = 1 To UBound(aGetallen) - AantalLoops  
into : For i = 1 To UBound(aGetallen)
and now it seems to work.

Before i had a partially-sorted arra , lik you said.

I will try youre code anyway.

14-01-2008 at 09:30 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : sort numeric array
Next Topic (numeric keypad) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder