Simple sort procedure does not sort the first item.

  • Thread starter Thread starter Head_one
  • Start date Start date
H

Head_one

Guest
Hello everyone,

This is a simple insertion sort procedure. It's bug: Never sorts the first string in array.

Private Sub Insert_Sort(ByRef Arr() As String)
Dim n, J As Integer
Dim s As String
For n = 1 To Arr.Length - 1 ' from 2nd cell to last one
s = Arr(n)
J = n - 1 'points one above n
While J > 0 And Arr(J) > s
Arr(J + 1) = Arr(J)
J = J - 1 ' pointer on previous line
End While
Arr(J + 1) = s
Next
End Sub


In my opinion: The While line should be:

While J >= 0 And Arr(J) > s

But in this case, I get a run time error: J=-1

The input array are the strings:

Line-9
Line-4
Line-7
Line-1
Line-8
Line-6
Line-3
Line-2
Line-5

Thanks to all Helpers.

Moshe.



All things are difficult before they are easy. (unknown)

Continue reading...
 
Back
Top