beginner50 minLección 7 de 8

Common Algorithm Patterns

Explore common algorithm patterns including search, sort, filter, transform, and aggregate with conceptual examples.

Common Algorithm Patterns

Many problems share similar structures. By recognizing these patterns, you can apply proven algorithmic solutions instead of reinventing the wheel. This lesson covers the five most common algorithm patterns you'll encounter.

The Five Common Patterns

100%
PatternWhat It DoesReal-World Example
SearchFinds a specific item in a collectionLooking up a contact in your phone
SortArranges items in a specific orderOrganizing books alphabetically
FilterSelects items that meet a conditionShowing only unread emails
TransformChanges each item in some wayConverting temperatures from C to F
AggregateCombines multiple items into a resultCalculating the total of a shopping cart

Pattern 1: Search

Search algorithms find a specific item (or determine it doesn't exist) within a collection of data.

Linear Search

The simplest search -- check every item until you find what you're looking for.

ALGORITHM: Linear Search INPUT: A collection of items, a target item to find OUTPUT: The position of the target, or "not found" STEP 1: SET index TO 0 STEP 2: WHILE index is less than the size of the collection DO IF collection[index] equals target THEN RETURN index END IF SET index TO index + 1 END WHILE STEP 3: RETURN "not found" END ALGORITHM
100%

When to Use Linear Search

SituationSuitable?Why
Small collection (under 50 items)YesFast enough, simple to implement
Unsorted dataYesOnly option without sorting first
One-time searchYesNo benefit from pre-sorting
Large collection, many searchesNoBinary search would be much faster

Binary Search

For sorted collections, binary search is dramatically faster. It works by repeatedly dividing the search space in half.

ALGORITHM: Binary Search INPUT: A sorted collection, a target item OUTPUT: The position of the target, or "not found" STEP 1: SET low TO 0 STEP 2: SET high TO size of collection - 1 STEP 3: WHILE low is less than or equal to high DO SET middle TO (low + high) divided by 2 IF collection[middle] equals target THEN RETURN middle ELSE IF collection[middle] is less than target THEN SET low TO middle + 1 ELSE SET high TO middle - 1 END IF END WHILE STEP 4: RETURN "not found" END ALGORITHM

Example trace: Finding 7 in [1, 3, 5, 7, 9, 11, 13]

Steplowhighmiddlecollection[middle]Action
10637Found! Return 3

Example trace: Finding 4 in [1, 3, 5, 7, 9, 11, 13]

Steplowhighmiddlecollection[middle]Action
106374 < 7, search left half
202134 > 3, search right half
322254 < 5, search left half
421----low > high, not found

Pattern 2: Sort

Sorting algorithms arrange items in a specific order (ascending, descending, alphabetical, etc.).

Bubble Sort

Bubble sort repeatedly compares adjacent items and swaps them if they're in the wrong order.

ALGORITHM: Bubble Sort INPUT: A list of numbers OUTPUT: The same list, sorted in ascending order STEP 1: SET n TO length of list STEP 2: REPEAT SET swapped TO false FOR i FROM 0 TO n - 2 DO IF list[i] is greater than list[i + 1] THEN SWAP list[i] and list[i + 1] SET swapped TO true END IF END FOR SET n TO n - 1 UNTIL swapped is false END ALGORITHM
100%

How Bubble Sort Works (Visual Trace)

Starting list: [5, 3, 8, 1, 2]

Pass 1:

  • Compare 5 and 3: swap -> [3, 5, 8, 1, 2]
  • Compare 5 and 8: no swap -> [3, 5, 8, 1, 2]
  • Compare 8 and 1: swap -> [3, 5, 1, 8, 2]
  • Compare 8 and 2: swap -> [3, 5, 1, 2, 8]
  • 8 "bubbles" to the end

Pass 2:

  • Compare 3 and 5: no swap -> [3, 5, 1, 2, 8]
  • Compare 5 and 1: swap -> [3, 1, 5, 2, 8]
  • Compare 5 and 2: swap -> [3, 1, 2, 5, 8]
  • 5 is now in position

Pass 3:

  • Compare 3 and 1: swap -> [1, 3, 2, 5, 8]
  • Compare 3 and 2: swap -> [1, 2, 3, 5, 8]
  • 3 is now in position

Pass 4:

  • Compare 1 and 2: no swap -> [1, 2, 3, 5, 8]
  • No swaps needed -- list is sorted!

Pattern 3: Filter

Filter algorithms select only the items that meet a specific condition, creating a smaller collection.

ALGORITHM: Filter INPUT: A collection of items, a condition OUTPUT: A new collection containing only items that meet the condition STEP 1: CREATE an empty collection called result STEP 2: FOR each item in the original collection DO IF item meets the condition THEN ADD item to result END IF END FOR STEP 3: RETURN result END ALGORITHM

Real-World Example: Filtering Products

ALGORITHM: Filter Products by Price INPUT: A list of products with prices, maximum price OUTPUT: List of products at or below the maximum price STEP 1: CREATE an empty list called affordable_products STEP 2: FOR each product in the product list DO IF product price is less than or equal to maximum price THEN ADD product to affordable_products END IF END FOR STEP 3: RETURN affordable_products END ALGORITHM
100%

Common Filter Conditions

Filter TypeConditionExample
RangeValue is between two boundsAge between 18 and 65
EqualityValue matches exactlyStatus equals "active"
ThresholdValue is above/below a pointPrice under R$100
PatternValue matches a patternName starts with "A"
MembershipValue is in a setCountry is in [BR, US, CA]

Pattern 4: Transform

Transform algorithms apply a change to every item in a collection, producing a new collection with modified items.

ALGORITHM: Transform (Map) INPUT: A collection of items, a transformation rule OUTPUT: A new collection with transformed items STEP 1: CREATE an empty collection called result STEP 2: FOR each item in the original collection DO SET transformed_item TO apply transformation to item ADD transformed_item to result END FOR STEP 3: RETURN result END ALGORITHM

Real-World Examples

Example 1: Converting Temperatures

ALGORITHM: Convert All Temperatures INPUT: A list of temperatures in Celsius OUTPUT: A list of temperatures in Fahrenheit STEP 1: CREATE an empty list called fahrenheit_temps STEP 2: FOR each temp_celsius in the input list DO SET temp_fahrenheit TO (temp_celsius * 9/5) + 32 ADD temp_fahrenheit to fahrenheit_temps END FOR STEP 3: RETURN fahrenheit_temps END ALGORITHM

Example 2: Formatting Names

ALGORITHM: Format Full Names INPUT: A list of people with first_name and last_name OUTPUT: A list of full names in "Last, First" format STEP 1: CREATE an empty list called formatted_names STEP 2: FOR each person in the input list DO SET full_name TO person.last_name + ", " + person.first_name ADD full_name to formatted_names END FOR STEP 3: RETURN formatted_names END ALGORITHM

Transform vs. Filter

AspectTransformFilter
Output sizeSame as inputSame or smaller than input
What changesThe items themselvesWhich items are included
Every item processed?YesYes (but some are discarded)
ExampleDouble every numberKeep only even numbers
100%

Pattern 5: Aggregate

Aggregate algorithms combine multiple items into a single result. Common aggregations include sum, count, average, maximum, and minimum.

ALGORITHM: Aggregate INPUT: A collection of items, an aggregation operation OUTPUT: A single combined result STEP 1: SET accumulator TO initial value (depends on operation) STEP 2: FOR each item in the collection DO UPDATE accumulator by combining it with item END FOR STEP 3: RETURN accumulator END ALGORITHM

Common Aggregation Operations

OperationInitial ValueUpdate RuleExample Result
Sum0accumulator = accumulator + item[1,2,3] -> 6
Product1accumulator = accumulator * item[1,2,3] -> 6
Count0accumulator = accumulator + 1[a,b,c] -> 3
MaximumFirst itemaccumulator = max(accumulator, item)[1,5,3] -> 5
MinimumFirst itemaccumulator = min(accumulator, item)[1,5,3] -> 1

Real-World Example: Shopping Cart Total

ALGORITHM: Calculate Cart Total INPUT: A shopping cart with items and prices OUTPUT: The total price STEP 1: SET total TO 0 STEP 2: FOR each item in the cart DO SET item_total TO item price multiplied by item quantity SET total TO total + item_total END FOR STEP 3: RETURN total END ALGORITHM

Combining Patterns

Real-world algorithms often combine multiple patterns:

ALGORITHM: Generate Sales Report INPUT: A list of all sales transactions OUTPUT: Total revenue from electronics over R$500 STEP 1: SET total_revenue TO 0 STEP 2: FOR each sale in the sales list DO IF sale category equals "Electronics" THEN IF sale amount is greater than 500 THEN SET total_revenue TO total_revenue + sale amount END IF END IF END FOR STEP 3: RETURN total_revenue END ALGORITHM

This algorithm combines:

  • Filter: Only electronics over R$500
  • Aggregate: Sum of filtered amounts
100%

Pattern Selection Guide

When facing a new problem, use this guide to identify which pattern applies:

100%

Practice Exercises

Exercise 1: Identify the Pattern

For each scenario, identify which pattern (or combination) applies:

  1. Finding the tallest student in a class
  2. Converting all prices from dollars to euros
  3. Finding all students who scored above 90
  4. Arranging books by publication date
  5. Calculating the average temperature for the month
  6. Finding a specific book by its ISBN, then updating its status

Exercise 2: Write a Search Algorithm

Write an algorithm that searches for a student by name in a list of student records. Each record has a name and a grade. Return the student's grade if found, or "Student not found" if not.

Exercise 3: Write a Filter + Transform

Write an algorithm that:

  • Takes a list of numbers
  • Filters out all negative numbers
  • Transforms the remaining numbers by squaring them
  • Returns the new list

Exercise 4: Write an Aggregate Algorithm

Write an algorithm that finds both the maximum and minimum values in a single pass through a list of numbers.

Exercise 5: Combine All Patterns

Design an algorithm for a library system that:

  1. Searches for books by a specific author
  2. Filters to only include books published after 2020
  3. Transforms each book record to show only title and year
  4. Aggregates to count how many matching books exist

Summary

In this lesson, you learned:

  • Search: Finding specific items (linear for unsorted, binary for sorted)
  • Sort: Arranging items in order (bubble sort as a simple example)
  • Filter: Selecting items that meet conditions
  • Transform: Changing each item in a collection
  • Aggregate: Combining multiple items into a single result
  • Combining patterns: Real-world algorithms often use multiple patterns together
Success

These five patterns are the building blocks of most algorithms you'll encounter. By recognizing which pattern applies to a problem, you can quickly design an effective solution.

Key Terms

TermDefinition
SearchFinding a specific item in a collection
Linear SearchChecking each item one by one
Binary SearchRepeatedly halving the search space in a sorted collection
SortArranging items in a specific order
Bubble SortA simple sort that swaps adjacent out-of-order items
FilterSelecting items that meet a condition
TransformApplying a change to every item
AggregateCombining multiple items into a single result
AccumulatorA variable that builds up a result during aggregation
Progreso88%