Excel VBA - Sort a table with BubbleSort

C

CGeorges

Hello,

I need to sort a table with BubbleSort.

I know how the algorithm works:

For i = left To right - 1
If student_temp(i) > student_temp(i + 1) Then
Call swap(i, i + 1)
End If


Private Sub swap
Dim hold As String

hold = student_temp(j)
student_temp(j) = student_temp(k)
student_temp(k) = hold


but I have no idea how this works on an Excel sheet. How do I access
the rows? Do I need an array?

I will appreciate any help.

Thanks in advance
 
B

Bob Phillips

Why do you need the bubble sort, why not use Excel's built-in sort?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
A

acw

Hi

One way is to put the range data into an array, perform the sort, then put the data back again. Something like.

Sub aaa()
Dim arr As Variant
arr = Range("a1:f1").Value

For i = LBound(arr, 2) To UBound(arr, 2) - 1
For j = i + 1 To UBound(arr, 2)
If arr(1, i) > arr(1, j) Then
holder = arr(1, i)
arr(1, i) = arr(1, j)
arr(1, j) = holder
End If
Next j
Next i

Range("a1:f1").Value = arr

End Sub

Tony

----- CGeorges > wrote: -----

Hello,

I need to sort a table with BubbleSort.

I know how the algorithm works:

For i = left To right - 1
If student_temp(i) > student_temp(i + 1) Then
Call swap(i, i + 1)
End If


Private Sub swap
Dim hold As String

hold = student_temp(j)
student_temp(j) = student_temp(k)
student_temp(k) = hold


but I have no idea how this works on an Excel sheet. How do I access
the rows? Do I need an array?

I will appreciate any help.

Thanks in advance
 
Top