Microbit Computer Science
Beginner
80 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC

Basic Sorting and Searching

In this lesson, you'll learn the essentials of sorting and searching algorithms, vital for organising and retrieving data efficiently. Follow step-by-step guidance to understand bubble sort, linear search, and binary search, and simulate them using MakeCode for micro:bit.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you'll explore the fundamentals of sorting and searching algorithms, which are key tools in computer science for organising data and finding information quickly. You'll gain an understanding of how these algorithms work, why they're important, and how to simulate them using MakeCode for the micro:bit.

    Here's what you'll cover:

    1. Understanding sorting, with a focus on the bubble sort algorithm.
    2. Practising bubble sort manually to see how it rearranges data step by step.
    3. Learning about searching algorithms, including linear search and binary search.
    4. Implementing a simple linear search in MakeCode to simulate sequential checking.
    5. Implementing a basic binary search in MakeCode to demonstrate efficient searching on sorted data.
    6. Discussing algorithm efficiency, comparing time complexities, and when to use each method.

    2 - Understanding Sorting

    Sorting is a fundamental concept in computer science that involves arranging items in a specific order, such as numerical or alphabetical. This makes data easier to search, analyse, and manage. For example, think about sorting a list of names in a phone book or numbers in a spreadsheet.

    In computer science, sorting algorithms are methods used to reorder elements. Some common sorting algorithms include:

    • Bubble Sort: A simple algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It's easy to understand but not very efficient for large lists.
    • Insertion Sort: Builds the sorted list one item at a time by inserting each new element into its correct position.
    • Quick Sort: A more efficient algorithm that divides the list into smaller parts and sorts them recursively.
    • Merge Sort: Divides the list into halves, sorts them, and then merges the sorted halves back together.

    Each algorithm has its strengths and weaknesses, often measured by efficiency (how fast it runs) and complexity. For this lesson we will focus on Bubble Sort.

    Why is sorting important? It prepares data for efficient searching and other operations. For instance, a sorted list allows for quicker searches, as we'll explore later.

    3 - Bubble Sort

    Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until the list is sorted. It gets its name because smaller elements 'bubble' to the top of the list (assuming ascending order).

    Key characteristics:

    • It performs multiple passes through the list.
    • In each pass, it compares pairs of adjacent items and swaps if necessary.
    • After each pass, the largest unsorted element is placed in its correct position.
    • It's easy to implement but inefficient for large lists due to its time complexity.

    Let's look at an example with a sample list: [5, 3, 8, 4, 2]. We'll sort it in ascending order.

    Pass 1:

    • Compare 5 and 3: 5 > 3, swap → [3, 5, 8, 4, 2]
    • Compare 5 and 8: 5 < 8, no swap
    • Compare 8 and 4: 8 > 4, swap → [3, 5, 4, 8, 2]
    • Compare 8 and 2: 8 > 2, swap → [3, 5, 4, 2, 8]

    Now, 8 is in its correct position.

    Pass 2: (on the first 4 elements)

    • Compare 3 and 5: 3 < 5, no swap
    • Compare 5 and 4: 5 > 4, swap → [3, 4, 5, 2, 8]
    • Compare 5 and 2: 5 > 2, swap → [3, 4, 2, 5, 8]

    Now, 5 is in its correct position.

    Pass 3: (on the first 3 elements)

    • Compare 3 and 4: 3 < 4, no swap
    • Compare 4 and 2: 4 > 2, swap → [3, 2, 4, 5, 8]

    Pass 4: (on the first 2 elements)

    • Compare 3 and 2: 3 > 2, swap → [2, 3, 4, 5, 8]

    No more swaps needed; the list is sorted.

    Here's some pseudocode for bubble sort:

    START

    SET list TO [5, 3, 8, 4, 2]

    REPEAT
          Go through the list from left to right
          Compare two neighbours at a time
          IF the left one is bigger THAN the right one THEN
                SWAP them
          END IF
          When you reach the end, that's one pass
          The biggest number should now be at the end
    UNTIL no more swaps happen

    END
    Reflect on this: How many comparisons were made in the example? For larger lists, why might this be inefficient?

    4 - Practical Task: Bubble Sort

    Now that you've learned about bubble sort, let's practise it manually to understand how it works. Get a sheet of paper and a pen.

    Your task:

    1. Write down this list of numbers: [7, 2, 9, 1, 5].
    2. Perform bubble sort on this list to sort it in ascending order. Go through each pass step by step, comparing adjacent numbers and swapping if necessary.
    3. Write down the list after each pass. For example, after the first pass, show what the list looks like.
    4. Continue until the list is fully sorted and no more swaps are needed.
    5. Count how many passes and swaps you made.
    Bubble Sort Answer

    Initial list: [7, 2, 9, 1, 5]

    Pass 1:

    • Compare 7 > 2: swap → [2, 7, 9, 1, 5]
    • Compare 7 < 9: no swap
    • Compare 9 > 1: swap → [2, 7, 1, 9, 5]
    • Compare 9 > 5: swap → [2, 7, 1, 5, 9]

    List after Pass 1: [2, 7, 1, 5, 9] (3 swaps)

    Pass 2:

    • Compare 2 < 7: no swap
    • Compare 7 > 1: swap → [2, 1, 7, 5, 9]
    • Compare 7 > 5: swap → [2, 1, 5, 7, 9]
    • Compare 7 < 9: no swap

    List after Pass 2: [2, 1, 5, 7, 9] (2 swaps)

    Pass 3:

    • Compare 2 > 1: swap → [1, 2, 5, 7, 9]
    • Compare 2 < 5: no swap
    • Compare 5 < 7: no swap
    • Compare 7 < 9: no swap

    List after Pass 3: [1, 2, 5, 7, 9] (1 swap)

    Pass 4:

    • All comparisons: no swaps

    List after Pass 4: [1, 2, 5, 7, 9] (0 swaps)

    Final sorted list: [1, 2, 5, 7, 9]

    Total passes: 4
    Total swaps: 6

    5 - Understanding Searching

    Searching is a key concept in computer science that involves finding a specific item within a collection of data, such as locating a name in a database or a word in a document. Efficient searching helps us retrieve information quickly from large datasets.

    In computer science, searching algorithms are methods used to locate items. Some common searching algorithms include:

    • Linear Search: A straightforward method that checks each item in the list one by one until the target is found or the list ends. It's simple but can be slow for large lists.
    • Binary Search: A more efficient algorithm that works on sorted lists by repeatedly dividing the search interval in half. If the target is not in the middle, it eliminates half the list and continues.

    Each algorithm has its advantages depending on the data structure and whether the data is sorted.

    Why is searching important? It enables quick data retrieval, which is crucial for applications like search engines, databases, and recommendation systems. Efficient searching, especially when combined with sorting, can significantly reduce the time needed to find information in large datasets.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more