0% found this document useful (0 votes)
26 views

Bubble Sort

Uploaded by

jokante1151
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Bubble Sort

Uploaded by

jokante1151
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

AS & A LEVEL COMPUTER SCIENCE 9618

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

BUBBLE SORT ALGORITHM (REPEAT UNTIL & FOR)


Task: Create an array called student marks and use it to store marks of 100 students. Write a pseudo
code to arrange student marks in ascending order using Repeat Until iteration
Algorithm
DECLARE MaxIndex : INTEGER
DECLARE Counter : INTEGER
DECLARE TempStore : INTEGER
DECLARE isSorted : BOOLEAN
DECLARE Boundary : INTEGER
Boundary MaxIndex-1

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

BUBBLE SORT ALGORITHM (NESTED FOR)


Task: Create an array called student marks and use it to store marks of 100 students. Write a pseudo
code to arrange student marks in ascending order using Repeat Until iteration
Algorithm
DECLARE MaxIndex : INTEGER
DECLARE CounterOut : INTEGER
DECLARE CounterInner : INTEGER
DECLARE TempStore : INTEGER

FOR CounterOut 1 To MaxIndex - 1


FOR CounterInner CounterOut + 1 TO MaxIndex - 1
IF StudentMarks[ Counter Inner] > StudentMarks[ CounterInner + 1 ]
THEN
TempStore StudentMarks[CounterInner]
StudentMarks[CounterInner] StudentMarks[CounterInner+1]
StudentMarks[CounterInner+1] TempStore
ENDIF
NEXT CounterInner
CounterInner CounterInner – 1 ---This means next time in the inner loop don’t
look at already sorted values
NEXT CounterOut

6 Davis_Kazibwe@2023KIS

You might also like