An algorithm is a series of steps that you follow to solve a problem or complete a task. Think of it like a recipe in cooking that tells you what to do, step by step.
Key Characteristics of Algorithms
Input - The data the user enters to start the process
Output - The result or solution based on the given inputs
Definiteness - Each step must be precise and clear
Finiteness - Must have a finite number of steps and eventually end
Effectiveness - Steps should be simple and executable
Language Independence - Can be implemented in any programming language
Types of Algorithms
Sorting Algorithms - Arrange elements in order (e.g., alphabetical)
Visual Comparison: Linear vs Non-Linear Data Structures
Other Classifications
Static vs Dynamic Data Structures
Static Data Structures:
Have a fixed size
Memory allocated at compile time
Size cannot be changed after compilation
Example: Arrays
Dynamic Data Structures:
Have a size that can change
Memory allocated at runtime
Can grow or shrink as needed
Example: Linked Lists
5. ARRAYS (Linear Data Structure)
What is an Array?
An array is a collection of elements stored in contiguous memory locations. Each element can be accessed by its index (position).
Think of it like: A market list where you neatly arrange items (Akple, Fufu, Kenkey, Banku, Red Red) in rows, making it easy to find, add, or swap items.
Array Structure Visualization
Example Array in Python
# Creating an array
foods = ["Akple", "Fufu", "Kenkey", "Banku"]
# Accessing elements by index (starts at 0)
print(foods[0]) # Output: Akple
print(foods[2]) # Output: Kenkey
Array Operations
Reverse - Reverse the order of elements
Sort - Arrange elements in order
Search - Find specific elements
Count - Count occurrences of an element
Copy - Duplicate the array
Example: Reversing an Array
activities = ["Morning jog", "Breakfast", "Study"]
print("Original:", activities)
# Reverse the array
activities.reverse()
# Output each element
for activity in activities:
print(activity)
Output:
Original: ['Morning jog', 'Breakfast', 'Study']
Study
Breakfast
Morning jog
Description: Actual programming language (like Python)
Best For: Computer execution
Advantage: Can be run by computer, produces actual results
Key Insight
All three formats achieve the SAME GOAL - they just present the solution in different ways!
Process: Pseudocode → Flowchart → Code
7. SWAP ALGORITHM
What is Swapping?
The swap algorithm is used to exchange the values of two variables without losing data.
Real-World Analogy
Imagine you have two cups:
Cup A has water (🌊)
Cup B has juice (🧃)
To swap without spilling:
Get an empty cup (Temp)
Pour water from Cup A into Temp
Pour juice from Cup B into Cup A
Pour water from Temp into Cup B
Now: Cup A has juice, Cup B has water!
Swap Algorithm Steps
Temp = A - Store first value in temporary variable
A = B - Copy second value to first variable
B = Temp - Copy temporary value to second variable
Python Code Example
# Swap Algorithm
a = 49
b = 61
print("Original values:", a, "and", b)
# Swap using temporary variable
temp = a
a = b
b = temp
print("After swap:", a, "and", b)
Output:
Original values: 49 and 61
After swap: 61 and 49
Why Use a Temporary Variable?
Without a temporary variable, we would lose the original value of A when we assign B to it!
Visual Representation: Swap Algorithm
KEY CONCEPTS SUMMARY
✅ Variables - Storage containers that hold values which can change
✅ Algorithms - Step-by-step instructions to solve problems
✅ Flowcharts - Visual representation using standard shapes
✅ Data Structures - Organize and manage data efficiently
✅ Linear Data Structures - Elements arranged sequentially (e.g., Arrays)
✅ Non-Linear Data Structures - Elements interconnected (e.g., Trees, Graphs)
✅ Swap Algorithm - Exchange values using a temporary variable
✅ Three Representations - Pseudocode, Flowchart, and Code all solve the same problem
REMEMBER
Learning to code is like learning a new language - it requires practice and persistence!
Stay curious, ask questions, and keep practicing these fundamental concepts to become a skilled programmer.