Ce tri d'insertion est pour trier le tableau dans l'ordre croissant, quand il tente de le faire, je reçois un index hors gamme, quand cela se produit "j" est 0 et "i" est 1. Il essaie de comparer la valeur dans le premier élément à la valeur dans l'élément avec un indice de "-1" qui n'existe pas. Quels changements puis-je faire pour rendre ce code fonctionnel?Pourquoi est-ce que je reçois une exception d'index hors plage sur mon tri d'insertion?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim i, j, element, length As Integer
Dim array(7) As Integer
array(0) = 5
array(1) = 2
array(2) = 7
array(3) = 6
array(4) = 9
array(5) = 1
array(6) = 4
array(7) = 8
length = array.Length
For i = 1 To length - 1
j = i
While j > 0 And array(j) < array(j - 1)
If array(j - 1) > array(j) Then
element = array(j)
array(j) = array(j - 1)
j = j - 1
array(j) = element
End If
End While
Next
For Index As Integer = 0 To 7
ListBox1.Items.Add(array(Index))
Next
End Sub