Sort Code does not return correct value

1

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

My problem is that I'm throwing the items from the list of random numbers into an array, implementing the computer method and putting the ordered numbers into a second list.

But when I'm doing this, it always returns the number 0, or the int32 [] array error. 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 array of random numbers, VL is the array of numbers already sorted and lst is the listbox for which numbers are sorted.

    
asked by anonymous 04.07.2016 / 14:49

2 answers

2

Well, this one has 2 unbootable variables ... the min and max ... has a similar program that I did months ago that can be useful for you ... in this case my vector has 99 even number random numbers that in the case already are put in the for Note: the code is in vba, but the structure is very 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
    
04.07.2016 / 15:16
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

Would this be basically? , but is now giving error in the following part

  If ve(j) < best_value Then
  An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Computers22.exe

    
04.07.2016 / 19:11