Sort algorithm

wittyteacup

New Member
Can anyone figure out why this bubble sort algorithm doesn'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<n-1<BR> for j=0 TO j<n-1-i<BR> if arrToSort(j+1) < arrToSort(j) 'compare the two neighbors <BR> tmp = arrToSort(j) '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't figure out why!is there missing an THEN in the if loop?It still doesn't work..What is this?<BR><BR>for i=0 TO i<n-1<BR>for j=0 TO j<n-1-i<BR><BR>Don't you just mean<BR><BR>for i=0 TO n-1<BR>for j=0 TO n-1-i<BR>That's exactly what I mean :-) Thanks!
 
Back
Top