Bubble sort

This code shows how the numbers can be sorted in ascending order using the bubble sort method.


Private Sub BubbleSort()
Dim intAry(9) As Integer
Dim intI, intJ, intTemp As Integer
Dim strOutput As String = ""

'Consider a array which stores numeric values.
intAry(0) = 98
intAry(1) = 8
intAry(2) = 87
intAry(3) = 10
intAry(4) = 28
intAry(5) = 75
intAry(6) = 63
intAry(7) = 59
intAry(8) = 49
intAry(9) = 24

'Consider two numbers i.e. n and n+1
For intI = 0 To 9
For intJ = intI + 1 To 9
If intAry(intJ) < intAry(intI) Then
'If the number n+1 is less than the number n then swap
'their positions.For swapping intTemp is used.
intTemp = intAry(intI)
intAry(intI) = intAry(intJ)
intAry(intJ) = intTemp
End If
Next
Next
'The final output is saved in variable strOutput.
For intI = 0 To 9
If strOutput.Length = 0 Then
strOutput = intAry(intI)
Else
strOutput = strOutput & "," & intAry(intI)
End If
Next
MessageBox.Show(strOutput)
End Sub


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: