Sorting Code does not return correct value

Asked

Viewed 72 times

1

I have a class assignment whose need is to use an sorting algorithm to sort a list of random numbers.

My problem is that I am throwing the items from the random number list into an array, implementing the sort method and placing the ordered numbers into a second list.

But when I am doing this it always returns the number 0, or the error int32[] array. How should I proceed?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim min As Integer
    Dim max As Integer
    Dim i As Integer
    Dim j As Integer
    Dim best_value As Long
    Dim best_j As Integer

    For i = min To max - 1
        best_value = ve(i)
        best_j = i
        For j = i + 1 To max
            If ve(j) < best_value Then
                best_value = ve(j)
                best_j = j
            End If
        Next j
        vl(best_j) = vl(i)
        vl(i) = best_value

    Next i

    Dim h As Integer
    For h = 0 To 1
        Lst.Items.Add(vl(h))

    Next h

End Sub

In this code VE is the random number array, VL is the array of numbers already sorted and lst is the listbox to which the ordered numbers go.

  • 1

    A valuable tip: use more descriptive names for their variables. Names like X, Y, VE, etc. are not good at all.

  • Thank you, I will follow your tips =D

  • 1

    Talison, your code is not initializing the max variable, so the loop (For i = min To max - 1) will never run. Check this and if the problem persists put the modified code to help you.

2 answers

2

well yours is with 2 non-initialized variables... min and max... has a similar program that I did months ago that may be useful for you... in this case my vector has 99 random pairs that in case are already launched in the Obs: the code is in vba, but the structure is quite similar to Vb.net

Sub ptnaprova2()

      Dim vet(99) As Integer   Dim x, y, n, aux, menor As Integer
         For x = 0 To 99
        n = Int(Rnd() * 1000) + 1
        If n Mod 2 = 0 Then
          vet(x) = n
        Else
          vet(x) = vet(x) + 1
        End If   Next x

        For x = 0 To 99
          For y = (x + 1) To 99
          If vet(x) > vet(y) Then
            aux = vet(x)
            vet(x) = vet(y)
            vet(y) = aux
          End If
        Next y
          Debug.Print aux
        Next x
         End Sub

1

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


    Dim i As Integer
    Dim j As Integer
    Dim best_value As Long
    Dim best_j As Integer

    For i = 0 To ve.Length - 1
        best_value = ve(i)
        best_j = i
        For j = i + 1 To ve.Length
            If ve(j) < best_value Then
                best_value = ve(j)
                best_j = j
            End If
        Next j
        vl(best_j) = vl(i)
        vl(i) = best_value

    Next i

    Dim h As Integer
    For h = 0 To 1
        Lst.Items.Add(vl(h))

    Next h

End Sub

Basically it would be this ? , but now this giving error in the following part

  If ve(j) < best_value Then

An unhandled Exception of type 'System.Indexoutofrangeexception' occurred in Ordenadores22.exe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.