Re: Problem with this text-sorting algorithm
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Sun, 11 Mar 2007 09:41:59 -0400
"Andrew" <andrewmrichards@xxxxxxxxxxx> wrote in message
news:1173618639.109017.32610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi all
I need to write an algorithm which will sort an array of text strings
into alphabetical order.
I wrote the procedure below, which seemed to do the job just fine, but
during the course of testing, it seemed, on one (and only one)
occasion to miss an entry. In other words, I fed in an array of 12 or
so strings, all of which sorted perfectly, except that the last entry
began with the letter F. All others were in perfect order, it just
seemed to have "missed" one.
Now, unfortunately, I have no idea what the original order was,
because I didn't, of course, know there would be a problem until after
I'd done the sort. I've tried many times to re-create the problem, but
with no luck - whatever array I provide is sorted perfectly. So I'm
left looking at the logic of the procedure to spot where it may have
gone wrong, and again, I can't see a problem. This might be because
there is no problem - maybe I dreamt it.... or it may be that I can't
see my own logic error.
Therefore, if anyone can take a look at the code below and spot any
potential flaws, I'd be very grateful.
Many thanks
Andrew
------------------------------------------
Sub TextSort(ByRef line() As String, ByVal NumOfElements As Short)
Dim temp As String
Dim i As Short, loopup As Short
For i = 1 To NumOfElements
For loopup = i + 1 To NumOfElements
If line(i) > line(loopup) Then
temp = line(loopup)
line(loopup) = line(i)
line(i) = temp
End If
Next
Next
End Sub
You should really be asking in a dotnet newsgroup. This one's for VB6 and
under. It's clear you're using VB.NET because of the Short data type, which
VB6 doesn't have.
Your loop counter should probably start with 0, not 1.
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: Problem with this text-sorting algorithm
- From: Andrew
- Re: Problem with this text-sorting algorithm
- From: Bob Butler
- Re: Problem with this text-sorting algorithm
- References:
- Problem with this text-sorting algorithm
- From: Andrew
- Problem with this text-sorting algorithm
- Prev by Date: Re: Problem with this text-sorting algorithm
- Next by Date: Re: Problem with this text-sorting algorithm
- Previous by thread: Re: Problem with this text-sorting algorithm
- Next by thread: Re: Problem with this text-sorting algorithm
- Index(es):
Relevant Pages
|