Bubble sort is a simple algorithm that compares based in which each pair of adjacent elements is compared. the values are then swapped when they are not in order. However this is not suitable for large sets of data as it is not a quick method for sorting.
Algorithm
for all elements of the list
if list(i) > list(i + 1)
swap(list(i) = list(i + 1))
end if
end for
return list
end bubble sort
Pseudo-code
repeat
swapped = false
for i = 1 to array - 1 do
if A(i) > A(i + 1) then
temp = A(i)
swap A(i) = A(i + 1)
A(i + 1) = temp
swapped = true
end if
Code
Module Module1
'Callum Westcott
'These are my variables
Dim swap As Boolean
Dim num(8) As Integer
Dim temp As Integer
Sub Main()
'these are the values for the array
num(1) = 20
num(2) = 72
num(3) = 69
num(4) = 88
num(5) = 42
num(6) = 169
num(7) = 101
num(8) = 99
'this do loop will make sure that all of the number will show in order
Do
swap = False
For index = 1 To 8 - 1
If num(index) > num(index + 1) Then
temp = num(index)
num(index) = num(index + 1)
num(index + 1) = temp
swap = True
End If
Next
Loop Until swap = False
'this will display the ordered array
For index = 1 To 8
Console.WriteLine(num(index))
Next
Console.ReadLine()
End Sub
End Module
No comments:
Post a Comment