# function bubble sort three numbers

#### nathanfroese

##### New Member
i need help any example post on bubble sort function not a sub that can sort three numbers and display it in any way via the three sorting cells or in the function cell using only single array dimension. also the function should use as double for all numbers to sort.

here the sample question from our work shop:
function using a form of the bubble sort algorithm which we
explored in a previous workshop. I suggest you start with the Matlab code that you wrote
previously and consider how you can convert it into VBA. You may wish to simplify the algorithm
since you only have three numbers to sort. If you set your outer loop to cycle through each pair of​
numbers twice this will ensure that you have correctly sorted the three numbers.

plz need help asap.

#### BrianB

##### Well-known Member
This is the principle.
Code:
``````'======================================================================================
'- BUBBLE SORT EXAMPLE : 3 NUMBERS (ASCENDING)
'- The method is to use a "pointer" and check its current position in the array.
'- If the current number is more than the next then switch their positions in the array.
'- If a position is changed then set a marker so the pointer goes through again.
'- The sort is complete when the pointer has gone through the array without change.
'- Brian Baulsom October 2008
'=====================================================================================

Sub SORT_()
Dim MyNumbers(3)
Dim Pointer As Integer
Dim Changed As Boolean
Dim MyTemp
'-------------------------------------------------------------------------------
MyNumbers(1) = 30
MyNumbers(2) = 20
MyNumbers(3) = 10
'-------------------------------------------------------------------------------
Do
Changed = False
For Pointer = 1 To 3 - 1
If MyNumbers(Pointer) > MyNumbers(Pointer + 1) Then
MyTemp = MyNumbers(Pointer)
MyNumbers(Pointer) = MyNumbers(Pointer + 1)
MyNumbers(Pointer + 1) = MyTemp
Changed = True
End If
Next
Loop While Changed = True
'---------------------------------------------------------------------------------
MsgBox (MyNumbers(1) & vbCr & MyNumbers(2) & vbCr & MyNumbers(3) & vbCr)
End Sub
'=====================================================================================``````

1,082,344
Messages
5,364,803
Members
400,814
Latest member
gangstar67

### This Week's Hot Topics

• populate from drop list with multiple tables
Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
• Find list of words from sheet2 in sheet1 before a comma and extract text vba
Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
• Dynamic Formula entry - VBA code sought
Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...