function bubble sort three numbers

nathanfroese

New Member
Joined
Oct 20, 2008
Messages
2
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.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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
'=====================================================================================
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top