Sort Search Questions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIT 1 - PROBLEM SOLVING

3 SORTING AND SEARHING ALGORITHMS


1. (3-b)Explain one drawback of using the merge sort algorithm to sort large data sets.[June -
2019]
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………

2. (5-b) Cerys will need an algorithm to search the table of stock data.
(i) State two advantages of using a linear search algorithm rather than a binary search
algorithm.[June-2020]
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(ii) ) State two disadvantages of using a linear search algorithm rather than a binary search
algorithm.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………

3. (4-b) Zak plans to implement a binary search algorithm to search file extension for the
programming language. [June-2021]
(i) Explain one advantage of a binary search compared to a linear search.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(ii) Figure 2 shows an algorithm for a binary search.
1 #initialise variables
2 SET productSearch TO “ “
3 SET startPosition TO 1
4 SET midPosition TO 0
5 SET endPosition TO LENGTH(productList)
6 SET found TO False
7
8 # Print prompts, take and check input from user
9 SEND “Enter the product code” TO DISLAY
10 RECEIVE productSearch FROM (STRING) KEYBOARD
11 REPEAT
12 mid Position = (startPositon +endPosition) DIV 2
13 IF productList [midPosition] < productSearch THEN
14 startPosition = midPosition + 1
15 END IF
16 IF productList [midPosition] > prodcutSearch THEN
17 endPosition = midPosition -1
18 END IF
19 UNTIL productList[midPosition] = productSearch OR startPosition = endPosition

This binary search algorithm will be used to search the product list for the product code str15.
Complete the table to indicate the order in which the product codes will be examined by the
algorithm.
Write the number 1 by the first product code to be examined, 2 by the second code to be
examined, and so on.

Position in list Product code Order examined


1 ark11
2 asp11
3 bar13
4 dri15
5 mil19
6 rib10
7 str15
8 tor16

(iii) Zak has another list containing the names of five students who attend the after-school club.

Give the maximum number of names that would need to be examined by the binary search
algorithm to determine whether a name appears in the list.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(iv) Give the name of an algorithm that could be used to sort a list.
….………………………………………………………………………………………………………………………………
4. (5-b).Figure 3 shows an array that stores player scores after a game. [June - 2022]
3 4 10 8 1 9
(i) Julia uses a bubble sort algorithm to sort the scores.
Complete the table to show how the bubbles sort algorithm will sort the scores.
You may not need to use all the rows.
3 2 10 8 1 9

(ii) Explain one reason why bubble sort is very efficient in terms of memory usage.

….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(c) Describe the steps a linear search algorithm takes to find a search item.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
5. (2) Estelle is learning about sorting and searching algorithm. [November - 2021]
(a) Estelle’s friend has given her a partially completed program to carry out a binary search.
This pseudocode contains the logic required to complete the program.

11 RECEIVE item FROM (INTEGER) KEYBOARD


12 WHILE start <= end AND NOT found Do
13 SET middle TO (start + end) DIV 2
14 IF numberList[middle] = item THEN#
15 SET found TO True
16 ELSE
17 IF item <numberList[middle] THEN
18 end =middle - 1
19 ELSE
20 Start = middle +1
21 END IF
22 END IF
23 SET count TO count + 1
24 END WHILE

Open file Q02a in the code editor.


Amend the code to complete the program.
You must use the structure given in Q02a to write the program.
Do not add any further functionality. Save your code as Q02aFINISHED with the correct file
extension for the programming language.
[Write your code in given lines]
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(b) Figure 3 shows an array of names.
0 1 2 3 4 5 6 7
Yekatrina Juan George Elija Christine Sangeetha Melanie Shu-fen
Explain why a binary search algorithm would not found ‘Juan’ in the array.

….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(c) A merge sort algorithm can be used to sort a list into ascending order.
The list 9, 1, 7, 6, 3, 5, 2, 8 needs to be sorted.
Complete the merge sort using the space provided

9,1,7,6,3,5,2,8
6. (specimen-4) A list of numbers to be sorted using a bubble sort algorithm.
a) Give a definition of the term ‘algorithm’.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
b) Here is a list of numbers that need to be sorted in ascending order
28 7 26 21 34 18 16 9

Perform the first pass of a bubble sort.


Use this space for working to help you answer the questions.

(i) Complete the table to show how the list will have changed at the end of the first pass.

(ii) State the number of comparisons made in the first pass.


….………………………………………………………………………………………………………………………………
(iii) State the number of swaps made in the first pass.
….………………………………………………………………………………………………………………………………
(c) A bubble sort is only one type of sorting algorithm.

(i) Give one reason why a bubble sort is inefficient when sorting a large dataset.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
(ii) State the position in a list that will always remain unchanged after the first pass of any
ascending order bubble sort.
….………………………………………………………………………………………………………………………………
Answers:
1. More memory/storage required because not in place/ many steps
divisions of data set/ is recursive

2.
(i)Simple implementation
Can be used for sorted / unsorted list
If the target is at the beginning of the data structure the search will be faster than a binary
search.
(iii) May need to compare with all items in list before search complete.
May need longer time for searching a large list.

3.
(i)
⚫ Binary search can be quicker / is more effective than a linear search / is more efffective
with larger lists as it does not have to examine each item in the list.
⚫ Binary search halves the list each time so it can faster to find an item
⚫ Binary search requires fewer comparisons than a linear search to establish an item is not
in the list because the linear list would need to compare each item before establishing
this
(ii)
Position in list Product code Order examined
1 ark11
2 asp11
3 bar13
4 dri15 1
5 mil19
6 rib10 2
7 str15 3
8 tor16

(iii) 3
(iv) Bubble sort / merge sort

4.
(4-b-i)
3 2 10 8 1 9
2 3 1 8 9 10
2 1 3 8 9 10
1 2 3 8 9 10

(4-b-ii)
⚫ The sorting is done in the same place as the original data which means only one additional
variable is needed to store the value being swapped ( when the values are out of order )
⚫ All sort and swapping operations are done on the original data which means the amount of
memory needed is constant
⚫ The list does not need to be split which would use the memory
(4-c)
⚫ Start at the beginning of the list
⚫ Compare each value in the list with the each item.
⚫ Repeat until a match is found / the end of the list is reached.

(5-a)

(5-b)
⚫ The list is not sorted Juan should come after Elija if the list was sorted/ Elija is compared
with Juan so the bottom half of the list would be discarded after the first pass through
the loop
⚫ Binary search works on a sorted list discarded each pass through based on comparison
of search item with middle item.
(5-c)
9 1 7 3 5 2 8

9,1,7,6 3,5,2,8
9,1 7,6 3,5 2,8
9 1 7 6 3 5 2 8
1,9 6,7 3,5 2,8
1,6,7,9 2,3,5,8
1 2 3 5 6 7 8 9

(6-a)
⚫ A step by step procedure (which if followed precisely with a given input produces a
predictable output.)
⚫ A list of instructions followed in sequence ( to solve a problem)
⚫ A process or set of rules to be followed ( to archive a predictable result)
(6-b-i)
7 26 21 28 18 16 9 34

(6-b-ii) 7
(6-b-iii) 6
(6-c-i)
⚫ requires many passes to complete the sort
⚫ Requires many comparisons/every number is compared every single pass
(6-c-ii) Top/Highest/right-most/last

Question Bank

QB1: A teacher has stored learner surnames as shown below.


Marek Jackson Bachchan Wilson Abraham French smith
Identify the stages of bubble sort when applied to this data.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
QB2: The teacher has a sorted list of names from another class as shown below
Azikiwe Bloom Byrne Davidson Gateri Hinton Jackson Linton Smith Wall
Identify the stages of a binary search to find the name ‘Jackson’ when applied to this list.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
QB3: A list is made up of the numbers 4,1,2,6,3,5
Identify the steps involved when sorting this list using a bubble sort algorithm.
QB4: what is the bubble sort?
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
QB5: Write 2 benefits of bubble sort.
….………………………………………………………………………………………………………………………………
….………………………………………………………………………………………………………………………………
QB6: Following algorithm is written for bubble sort, not in correct order. Arrange them in
correct order. First done is done for you.
A Compare the first two elements. If the first element is greater than the second
element, swap them.
B Continue this process, comparing and swapping adjacent elements until you reach the
end of the list.
C Start at the beginning of the list. 1
D Repeat steps 1-5 for each remaining element in the list, excluding the last (already
sorted) elements after each pass.
E Move to the next pair of elements and compare them in the same way.
F After completing one pass through the list, the largest element will "bubble up" to the
end of the list.
G Continue these passes until no more swaps are needed, indicating that the list is fully
sorted.
Answers:
QB1:
Marek,Jackson,Bachchan,Wilson,Abraham,French,smith
Jackson, Bachchan, Marek, Abraham, French, Smith, Wilson
Bachchan, Jackson, Abraham, French, Marek, Smith, Wilson
Bachchan, Abraham, French, Jackson, Marek, Smith, Wilson
Abraham, Bachchan, French, Jackson, Marek, Smith, Wilson

QB2:
Azikiwe, Bloom, Byrne, Davidson, Gateri, Hinton, Jackson, Linton, Smith, Wall
Median is Hinton: Jackson > Hinton
First half of list discarded. New list: Hinton, Jackson, Linton, Smith, Wall
Median is Linton: Jackson < Linton
Second half of list discarded. New list: Hinton, Jackson
Median is Jackson: search item found

QB3:
Pass 1 Pass 2
142635 124356
124635 124356
124635 123456
124365
124356

QB4:
It is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order. This process is repeated until the list is sorted. It's called
"bubble" sort because smaller elements "bubble" to the top of the list with each pass through it. It's easy
to understand but not very efficient for large lists compared to other sorting algorithms.

QB5:
⚫ Simple to understand ( simplicity)
⚫ easy to implement
⚫ requires only a constant amount of extra space as it operates on the input list itself. ( this can be
beneficial in situations where memory usage is a concern)
⚫ is adaptive ( it performs well if the list is already partially sorted. In such cases, It needs fewer
comparisons and swaps to sort the list)
⚫ Stable sorting. ( it preserves the relative order of equal elements. It important in some applications
where the original order of equal elements must be maintained.)
QB6:
A Compare the first two elements. If the first element is greater than the second element, swap 2
them.
B Continue this process, comparing and swapping adjacent elements until you reach the end of the 4
list.
C Start at the beginning of the list. 1
D Repeat steps 1-5 for each remaining element in the list, excluding the last (already sorted) 6
elements after each pass.
E Move to the next pair of elements and compare them in the same way. 3
F After completing one pass through the list, the largest element will "bubble up" to the end of the 5
list.
G Continue these passes until no more swaps are needed, indicating that the list is fully sorted. 7

You might also like