š Data Structures and Algorithms
Master Data Structures and Algorithms!
Learn how to organize data efficiently and solve problems like a pro!
What are Data Structures?
Data structures are ways to organize and store data so we can use it efficiently. Think of them like different containers - a shopping list (array), a contact book (object), or a queue at a mamak stall!
Choosing the right data structure is like choosing the right tool - you wouldn't use a spoon to cut nasi lemak!
Key Concepts
Arrays
Ordered list of items. Like a queue at McDonald's - everyone has a position!
Objects/JSON
Key-value pairs. Like a dictionary - look up words to get definitions!
Sorting
Arranging data in order. Like organizing apps alphabetically on your phone!
Searching
Finding specific data. Like searching for a contact in your phone!
Big O Notation
Measures algorithm efficiency. How fast does it run with lots of data?
Optimization
Making code faster and more efficient. Work smarter, not harder!
Arrays and Array Methods
š Working with Arrays
Arrays store ordered lists of data. Here are essential array methods:
Objects and JSON
šļø Working with Objects
Objects store key-value pairs - perfect for structured data:
Bubble Sort Algorithm
š Bubble Sort - Simple but Slow
Bubble sort compares adjacent items and swaps them if they're in the wrong order. Like bubbles rising to the top!
[5, 2, 8, 1] ā Compare 5 & 2, swap ā [2, 5, 8, 1]
[2, 5, 8, 1] ā Compare 5 & 8, no swap
[2, 5, 8, 1] ā Compare 8 & 1, swap ā [2, 5, 1, 8]
...continues until fully sorted!
Selection Sort Algorithm
šÆ Selection Sort - Find the Smallest
Selection sort finds the smallest element and moves it to the front. Repeat for the rest!
Linear Search Algorithm
š Linear Search - Check Every Item
Linear search checks each item one by one until it finds the target. Simple but can be slow!
Binary Search Algorithm
ā” Binary Search - Divide and Conquer
Binary search is much faster but requires a SORTED array. It eliminates half the data each step!
Step 1: Check middle (12) ā Found it!
Search for 25:
Step 1: Check middle (12), 25 > 12 ā Search right half [18, 25, 30]
Step 2: Check middle (25) ā Found it!
Big O Notation - Algorithm Efficiency
ā” Understanding Big O
Big O notation describes how an algorithm's performance scales with input size:
- O(1) - Constant: Always takes same time (accessing array by index)
- O(log n) - Logarithmic: Very fast (binary search)
- O(n) - Linear: Grows with input (linear search, forEach)
- O(n log n) - Fast sorting (JavaScript's built-in sort)
- O(n²) - Quadratic: Slow for large data (bubble sort, nested loops)
Common Mistakes to Avoid
ā ļø Watch Out For:
- Binary search on unsorted data: Must sort first!
- Modifying array while looping: Can cause bugs
- Using slow algorithms for large data: O(n²) is bad for 10,000+ items
- Forgetting to return -1: Search functions should return -1 when not found
- Not considering edge cases: Empty arrays, single items, duplicates
Summary
You've learned:
- ā Arrays store ordered lists with useful methods (push, pop, map, filter)
- ā Objects store key-value pairs, JSON converts between string and object
- ā Bubble sort and selection sort are O(n²) - simple but slow
- ā Linear search is O(n), binary search is O(log n) - much faster!
- ā Big O notation measures algorithm efficiency
- ā Choose the right data structure and algorithm for the job!
š® Interactive Challenge: Practice Algorithms!
š Challenge 1: Implement Bubble Sort
Write bubble sort to sort Malaysian state names alphabetically:
š Challenge 2: Search Algorithm
Implement binary search to find a price in a sorted array:
š Challenge 3: Array Methods
Use array methods to manipulate data:
šÆ Activity: Algorithm Performance Comparison
What You'll Do:
Compare the performance of different sorting and searching algorithms!
Instructions:
- Create an array of 100 random numbers
- Time how long bubble sort takes (use console.time())
- Time how long selection sort takes
- Time how long JavaScript's built-in sort() takes
- Compare the results - which is fastest?
- Try with 1000 numbers - what happens?
Bonus Challenges:
- Compare linear vs binary search speed
- Create visualizations showing how each algorithm works
- Test with different data types (strings, objects)
šŖ Practice Challenges
Challenge 1: Grab Route Optimization
Create a system to find the shortest route for a Grab driver:
- Array of destinations with distances: [{ name: "KLCC", distance: 5 }, ...]
- Sort destinations by distance (closest first)
- Filter out destinations more than 10km away
- Calculate total travel distance
- Use binary search to find a specific destination
Bonus: Add traffic factor - multiply distance by 1.5 during peak hours!
Challenge 2: E-Commerce Product Search
Build a product search and filter system:
- Array of products (name, price, category, rating)
- Implement search by name (linear search)
- Sort products by price (low to high, high to low)
- Filter by category and minimum rating
- Find products in price range using binary search
Challenge 3: Student Grade Analytics
Analyze student performance data:
- Array of students with grades: [{ name: "Ahmad", grades: [85, 90, 78] }, ...]
- Calculate average grade for each student (use map)
- Find students with average > 80 (use filter)
- Sort students by average grade
- Find top 3 students
- Implement search to find student by name
Challenge 4: Food Delivery Optimization
Create a food delivery management system:
- Array of orders: [{ id: 1, restaurant: "Mamak", customer: "Ahmad", distance: 3, time: 20 }, ...]
- Sort orders by delivery time (urgent first)
- Filter orders within 5km radius
- Group orders by restaurant (use objects)
- Calculate total delivery fees based on distance
- Implement binary search to find order by ID
Think about Big O: Which operations are O(1), O(n), or O(n²)?