In computer science, algorithms are a set of well-defined instructions or rules designed to solve a specific problem. They are essential in programming as they provide a step-by-step guide on how to perform a task efficiently. Understanding algorithms is crucial for developing logical thinking and problem-solving skills in the field of computer science.
Algorithm: A finite set of instructions or rules that, when followed, solves a particular problem or performs a specific task.
Example: A simple algorithm to add two numbers:
Start
Input num1, num2
sum = num1 + num2
Output sum
End
Pseudocode: A high-level description of an algorithm that uses a mixture of natural language and programming language syntax.
Example: Pseudocode to find the maximum of two numbers:
Start
Input num1, num2
if num1 > num2 then
max = num1
else
max = num2
Output max
End
Flowchart: A graphical representation of an algorithm that uses different shapes to represent different operations and connectors to show the flow of control.
Example: Flowchart to check if a number is even or odd:
graph LR
A[Start] --> B(Input number)
B --> C{Number % 2 == 0?}
C -->|Yes| D[Output "Even"]
C -->|No| E[Output "Odd"]
D --> F[End]
E --> F
Efficiency: Refers to the ability of an algorithm to use the least amount of resources, such as time and memory, to perform a task.
Complexity: The measure of how the time or space requirements of an algorithm grow as the input size increases.
Linear Search: An algorithm that sequentially checks each element in a list until the target element is found or the list is exhausted.
Example: Find the position of the element 5 in the list [2, 4, 6, 8, 5, 3]:
Start
List = [2, 4, 6, 8, 5, 3]
Target = 5
for i from 0 to length(List) - 1 do
if List[i] == Target then
Output "Element found at position " + i
Stop
Output "Element not found"
End
Binary Search: An efficient search algorithm that divides the search interval in half at each step.
Example: Find the position of the element 7 in the sorted list [2, 4, 6, 7, 8, 10]:
Start
List = [2, 4, 6, 7, 8, 10]
Target = 7
low = 0
high = length(List) - 1
while low <= high do
mid = (low + high) / 2
if List[mid] == Target then
Output "Element found at position " + mid
Stop
else if List[mid] < Target then
low = mid + 1
else
high = mid - 1
Output "Element not found"
End
Bubble Sort: A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
Example: Sort the list [5, 2, 8, 1, 6] in ascending order using Bubble Sort:
Start
List = [5, 2, 8, 1, 6]
n = length(List)
for i from 0 to n-1 do
for j from 0 to n-i-1 do
if List[j] > List[j+1] then
Swap List[j] and List[j+1]
Output Sorted List
End
Merge Sort: A divide-and-conquer algorithm that divides the list into smaller sublists, sorts them, and then merges them back together.
Example: Sort the list [4, 2, 7, 1, 5, 3, 6] in ascending order using Merge Sort:
Start
List = [4, 2, 7, 1, 5, 3, 6]
MergeSort(List)
Output Sorted List
End
Write a pseudocode for a linear search algorithm to find a specific element in a list.
Answer:
Start
Input List, Target
for i from 0 to length(List) - 1 do
if List[i] == Target then
Output "Element found at position " + i
Stop
Output "Element not found"
End
Explain the steps of a binary search algorithm to find an element in a sorted list.
Answer:
Describe the working principle of the Bubble Sort algorithm.
Answer: Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. It repeatedly passes through the list until no swaps are needed, indicating that the list is sorted.
Compare the efficiency of linear search and binary search algorithms.
Answer:
Implement a Merge Sort algorithm to sort the list [9, 3, 6, 1, 7, 4] in ascending order.
Answer:
Start
List = [9, 3, 6, 1, 7, 4]
MergeSort(List)
Output Sorted List
End
These revision notes provide a comprehensive understanding of algorithms for Grade 11 Computer Science students following the Kenyan CBC curriculum. Understanding these concepts will enhance problem-solving skills and prepare students for the national exams.
Create a free account to bookmark notes, download past papers, track your revision and get AI study help - free for Kenyan students.
Already have one? Log in
Join our list and we'll send fresh study notes and past papers straight to your inbox.