Sort algorithm

1. Member
Join Date
Dec 1969
Posts
92

## Sort algorithm

Can anyone figure out why this bubble sort algorithm doesn&#039;t work? Here is my function:<BR><BR>Function bubbleSort(arrToSort AS Array, n AS Integer)<BR> Dim i AS Integer<BR> Dim j AS Integer<BR> Dim tmp AS Integer<BR> <BR> for i=0 TO i&#060;n-1<BR> for j=0 TO j&#060;n-1-i<BR> if arrToSort(j+1) &#060; arrToSort(j) &#039;compare the two neighbors <BR> tmp = arrToSort(j) &#039;swap arrToSort[j] and arrToSort[j+1]<BR> arrToSort(j) = arrToSort(j+1)<BR> arrToSort(j+1) = tmp<BR> End If<BR> Next j<BR> Next i<BR> bubbleSort = arrToSort<BR>End Function<BR><BR>I make an array and send it to the function, like this:<BR>Dim a As Array<BR>a = Array.CreateInstance(GetType(Integer),3) <BR>a.SetValue(3, 0)<BR>a.SetValue(7, 1) <BR>a.SetValue(1, 2)<BR>a = bubbleSort(a, 3)<BR><BR>But it still comes back unsorted, and I can&#039;t figure out why!

2. Senior Member
Join Date
Dec 1969
Posts
113

## RE: Sort algorithm

is there missing an THEN in the if loop?

3. Member
Join Date
Dec 1969
Posts
92

## Still nothing...

It still doesn&#039;t work..

4. Senior Member
Join Date
Dec 1969
Posts
355

## RE: Sort algorithm

What is this?<BR><BR>for i=0 TO i&#060;n-1<BR>for j=0 TO j&#060;n-1-i<BR><BR>Don&#039;t you just mean<BR><BR>for i=0 TO n-1<BR>for j=0 TO n-1-i<BR>

5. Member
Join Date
Dec 1969
Posts
92

## RE: Sort algorithm

That&#039;s exactly what I mean :-) Thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•