Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

CH 7 BUBBLE SORT

Consider the following list of numbers:

[5, 2, 9, 3, 4, 1]

Please write the steps of the Bubble Sort algorithm in pseudo code to sort this list in ascending order.
Include all the necessary loops, comparisons, and swaps, and make sure to explain each step of the
algorithm as you go along.

Additionally, calculate the total number of comparisons and swaps required to sort the given list using
the Bubble Sort algorithm.

SOL :

FIRST = 1
LAST = 6

REPEAT
SWAP = FALSE
FOR Index FIRST TO LAST -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 INDEX
LAST = LAST -1
UNTIL (NOT SWAP) OR LAST = 1
CH7 LINER SEARCH

Consider the following array of integers:

[12, 45, 67, 23, 9, 6, 56, 78, 91, 34]

Write the steps of a Linear Search algorithm in pseudo code to search for a specific target value within
this array. Include all the necessary loops and comparisons. Assume the target value is 56. Explain the
algorithm step by step and determine if the target value is found in the array. If found, also provide the
index at which it is located. If not found, indicate that it's not present in the array.

Additionally, calculate the total number of comparisons required to search for the target value in the
given array using the Linear Search algorithm.

OUTPUT “ENTER A NUMBER“


INPUT NUMBER
FOUND = FALSE
COUNTER =1
REPEAT
IF NUMBER = INTEGERS [COUNTER]
THEN FOUND = TRUE
ELSE
COUNTER = COUNTER + 1
END IF
UNTIL FOUND OR COUNTER > SIZE OF INTEGER
IF FOUND
THEN
OUTPUT NUMBER “FOUND AT POSITION ”
COUNTER “IN THE LIST”
ELSE
OUTPUT NUMBER “NOT FOUND”
END IF

You might also like