0% found this document useful (0 votes)
6 views3 pages

Searching and Sorting Algorithms Worksheet

The document is a worksheet focused on searching and sorting algorithms, specifically binary search and bubble sort. It includes tasks for students to practice searching for names using binary search and sorting names using bubble sort and merge techniques. Additionally, it asks students to analyze the maximum number of items to examine and the time complexity of the binary search algorithm.

Uploaded by

humzaomar2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Searching and Sorting Algorithms Worksheet

The document is a worksheet focused on searching and sorting algorithms, specifically binary search and bubble sort. It includes tasks for students to practice searching for names using binary search and sorting names using bubble sort and merge techniques. Additionally, it asks students to analyze the maximum number of items to examine and the time complexity of the binary search algorithm.

Uploaded by

humzaomar2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet 3 Searching and sorting

Unit 8 Algorithms

Worksheet 3 Searching and sorting


Task 1

Use the 10 name cards for the activities in Task 1.


1. With the 10 name cards in a sequenced row, face down, use a binary search to search for
the name Emily
Leave the cards you have examined face up.
Which card did you turn up first?
Which card did you turn up second?
Which card did you turn up third?

2. Turn the cards face down again.

Now search for the name Sophie

Which cards did you turn up? Write them in order.

What is the maximum number of cards you will need to find any given name?

What is the maximum number of items you will have to examine if the list contains
● 8 items?
● 16 items?
● 32 items?
● 37 items?
● 64 items?
● 2n items?

What is the time complexity of the binary search algorithm? Explain your answer.

3. The bubble sort algorithm is given below. Write the pseudocode statements for “swap the
names”.
FOR i = 0 TO n - 1
FOR j = 0 TO (n – i - 2)
IF names [j] > names[j + 1]
swap the names
ENDIF
ENDFOR
ENDFOR

1
Worksheet 3 Searching and sorting
Unit 8 Algorithms

Task 2

Use 8 name cards for the merge activity in Task 2.


● Lay the cards out in a row, face up, in the following unsorted order:

Jessi Sophi Popp Ameli


Ava Olivia Isla Lily
ca e y a

● Read a card from each pair and write the lower value name to the merged list of 2 items
● Then write the other value to the merged list
● Do this for each pair
● Fill in the names below. The first two pairs have been done for you.

Jes So
Oli
Ava sic phi
via
a e

● You are now merging Ava, Jessica, Olivia, Sophie.


● Pick up the first card from each list, Ava and Olivia.
● Compare, and move Ava to the new merged list
● Pick up another card (Jessica) from the same sublist as Ava
● Compare Jessica with Olivia
● Move Jessica to new merged list
● Olivia and Sophie are in the correct sequence so write them to the merged list.
● Merge the second sublist in the same way. Fill in the boxes below

2
Worksheet 3 Searching and sorting
Unit 8 Algorithms

● Finally merge the two remaining lists in the same way.


● Fill in the boxes below

Common questions

Powered by AI

Merge sort involves ordering and merging smaller sublists. With pairs like Ava, Jessica, and Olivia, Sophie, you repeatedly identify the lowest-value card from each pair, moving them into a new list. Comparing Ava and Olivia, you place Ava first, then compare Jessica with Olivia, moving Jessica next, followed by Olivia and Sophie, which are already ordered. This systematic, two-at-a-time sorting ensures the final sequence is correctly ordered .

Grasping algorithmic design intricacies enhances sorting and searching by revealing more efficient methodologies, like employing binary and merge sort algorithms to reduce time complexity and resource usage. Insight into these principles allows for customized solutions addressing specific dataset characteristics, optimizing computational tasks beyond basic implementations .

The maximum number of items examined in binary search correlates with the list size as: 8 items require 3 checks, 16 need 4, 32 require 5, 37 need 5, and 64 require 6 checks. Generally, for a list of size `2n`, the number of items examined is `log2(n)`, illustrating the algorithm's logarithmic time complexity (O(log n)), which efficiently narrows down the search range by half each step .

For a binary search in a sorted set of 37 items, a maximum of 6 flips is needed to locate any name. This optimization arises from the binary search halving the dataset each iteration, significantly reducing the required comparisons for target identification, showcasing its O(log n) efficiency .

The pseudocode for swapping names within a bubble sort can be written as follows: `temp = names[j]; names[j] = names[j + 1]; names[j + 1] = temp;`. Here, the `temp` variable temporarily holds one of the names to facilitate the swap, ensuring names are correctly exchanged within the list during iteration .

Binary search surpasses linear search owing to its O(log n) time complexity versus linear search's O(n). In larger datasets, binary search optimizes performance by iteratively splitting and discarding irrelevant halves, ensuring exponentially fewer steps are needed than the sequential checks central to linear search .

Merge sort excels in efficiency due to its divide-and-conquer strategy, splitting lists into smaller segments for easier sorting and merging them back. Despite being recursive and requiring additional space for temporary arrays, it handles large, random data sets more quickly than simpler methods like bubble sort, which may perform poorly on longer lists due to repetitive pair-wise comparisons .

Merge sort's divide-and-conquer approach decomposes the problem into smaller, more manageable subparts, efficiently sorting through recursion and concurrent merging. This contrasts sharply with bubble sort, which relies on repetitive and often inefficient swaps, making merge sort preferable for larger datasets due to consistent logarithmic performance, minimizing resource usage on extensive data .

First, divide the 10 cards into two equal halves to start the binary search. Flip over the middle card (fifth card if starting from the first card). If 'Emily' is not there, determine if 'Emily' would be before or after this card alphabetically. Let’s say 'Emily' is after the middle card. Flip the middle card of the remaining five cards, and if 'Emily' is still not found, repeat the division with the new subset. This process shows binary search's efficiency with a time complexity of O(log n), significantly reducing the number of necessary checks compared to a linear search .

To merge using merge sort, start by comparing the first elements of each sublist. For example, if merging Ava, Jessica, Olivia, Sophie, compare Ava with Olivia. Since Ava is alphabetically first, it is moved to the new list. Then, compare the next element from Ava's sublist, Jessica, with Olivia, placing Jessica next. Continue this process until all items are merged into the main list .

You might also like