Bubble Sort
Bubble Sort
BUBBLE SORT
Is an algorithm to arrange array elements either in ascending or descending order.
Steps
1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
BUBBLE SORT
2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
BUBBLE SORT
3 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
SWAPPING VALUES
X = 70
Y = 90
TempStore = 70
X=Y
Y = TempStore
Whenever swapping we need to create a temporary storage to hold temporarily
one of the values to be swapped. This is because if we do a direct swapping one of
the values will be lost.
Direct swapping
X = 70
Y = 90
X = Y --------------> This will assign X to 90 hence the X value will be lost in the
process
4 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
Repeat
isSorted TRUE
FOR Counter 1 TO Boundary
IF StudentMarks[ Counter ] > StudentMarks[ Counter + 1 ]
THEN
TempStore StudentMarks[Counter]
StudentMarks[Counter] StudentMarks[Counter+1]
StudentMarks[Counter+1] TempStore
isSorted FALSE
NEXT Counter
Counter Counter - 1
Until isSorted = TRUE
5 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
6 Davis_Kazibwe@2023KIS