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:
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:
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.
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:
Let's look at an example with a sample list: [5, 3, 8, 4, 2]
. We'll sort it in ascending order.
Pass 1:
[3, 5, 8, 4, 2]
[3, 5, 4, 8, 2]
[3, 5, 4, 2, 8]
Now, 8 is in its correct position.
Pass 2: (on the first 4 elements)
[3, 4, 5, 2, 8]
[3, 4, 2, 5, 8]
Now, 5 is in its correct position.
Pass 3: (on the first 3 elements)
[3, 2, 4, 5, 8]
Pass 4: (on the first 2 elements)
[2, 3, 4, 5, 8]
No more swaps needed; the list is sorted.
Here's some pseudocode for 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:
[7, 2, 9, 1, 5]
.Initial list: [7, 2, 9, 1, 5]
Pass 1:
List after Pass 1: [2, 7, 1, 5, 9] (3 swaps)
Pass 2:
List after Pass 2: [2, 1, 5, 7, 9] (2 swaps)
Pass 3:
List after Pass 3: [1, 2, 5, 7, 9] (1 swap)
Pass 4:
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
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:
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.