|
| 1 | +Module Module1 |
| 2 | + Sub Main() |
| 3 | + Dim Array(10), count As Integer |
| 4 | + For count = 1 To 10 |
| 5 | + Array(count) = Console.ReadLine() |
| 6 | + Next |
| 7 | + Call Bubblesort(Array, 10) |
| 8 | + |
| 9 | + For count = 1 To 10 |
| 10 | + Console.WriteLine(Array(count)) |
| 11 | + Next |
| 12 | + Console.ReadKey() |
| 13 | + End Sub |
| 14 | + Sub Bubblesort(ByVal Array() As Integer, ByVal Length As Integer) |
| 15 | + Dim isSorted As Boolean |
| 16 | + Dim I As Integer |
| 17 | + Dim J As Integer |
| 18 | + Dim Temp As Integer |
| 19 | + isSorted = True |
| 20 | + For I = Length - 1 To 1 Step -1 'last element of array does'nt get sorted. |
| 21 | + isSorted = True |
| 22 | + For J = 0 To I '- 1 |
| 23 | + If Array(J) > Array(J + 1) Then ' Compare neighboring elements |
| 24 | + Temp = Array(J) |
| 25 | + Array(J) = Array(J + 1) |
| 26 | + Array(J + 1) = Temp |
| 27 | + isSorted = False |
| 28 | + End If |
| 29 | + Next |
| 30 | + If isSorted = True Then Exit For |
| 31 | + Next |
| 32 | + End Sub |
| 33 | +End Module |
0 commit comments