Algorithms For The Girlies (FREE SAMPLE)
Algorithms For The Girlies (FREE SAMPLE)
Algorithms For The Girlies (FREE SAMPLE)
MICHELLE LAWSON
M S
IT H
OR LIES
G
AL THE GIR
FOR
What’s in this Book?
Introduction
Welcome Message
How to Use This Book
Graph Algorithms
Exploring Graphs
Dijkstra's Algorithm
Network Flow
Dynamic Programming
Understanding Overlapping Problems
Memoization
Algorithm Design Techniques
Divide and Conquer
Greedy Algorithms
Backtracking
Advanced Topics
Machine Learning Algorithms
Cryptographic Algorithms
Quantum Algorithms
Real-World Applications
Algorithms in Everyday Tech
Algorithms in Finance and Business
Glossary
Further Resources
Books, websites, and online courses for
further learning.
When I first dipped my toes into the world of computer science, I was
intrigued by how algorithms shape almost everything we do, yet I
found the resources a bit... dry. So I thought, "Why not add a bit of
sparkle to this?" This book is my attempt to blend the world of
algorithms with our everyday love for fashion and beauty, making it
not just informative but also relatable and fun! The relatability makes
it all easier to remember too.
So, grab your favorite drink, find a cozy spot, and let’s dive into the
stylish world of algorithms together. Let the adventure begin!
Getting Started With
Algorithms
Understanding Algorithms
An algorithm is simply a set of instructions. Like a makeup tutorial.
An algorithm takes in some input, like raw data or user input, and
through a series of calculations, comparisons, and logical operations,
produces a desired output.
1. Problem Solving
Algorithms give us a systematic approach to solving problems. They
break down complex tasks into smaller, more manageable steps,
making it easier to apply efficient and effective solutions.
4. Scalability
Algorithms allow programs to handle larger and more complex
datasets or inputs. By utilizing efficient algorithms, programmers can
ensure that their software can scale and handle increased workloads
without sacrificing performance.
2
Basically, algorithms are necessary tools for programming. They
enable programmers (that’s us ) to solve problems efficiently,
improve performance, make reusable code, handle scalability, and
ensure correctness and reliability. Understanding and implementing
algorithms is essential for any programmer seeking to develop high-
quality software. To do this, programmers learn to think
algorithmically. But, what does this even mean?
Algorithmic Thinking
The concept of algorithmic thinking is similar to that of
organizing and executing a beauty or fashion routine.
Just as developing a skincare routine involves
designing specific steps to achieve healthy, glassy
skin, algorithmic thinking involves breaking down a problem or
desired outcome into manageable steps to find an effective solution.
Let's explore this using our skin care routine analogy:
Just like setting a goal for your skin type (e.g., hydration, acne
treatment, anti-aging), in algorithmic thinking, the first step is to
clearly define the problem or objective you want to achieve with your
algorithm.
3
4. Optimization
Just as you might choose a moisturizer with SPF to both hydrate and
protect from the sun, optimizing an algorithm involves finding the
most efficient way to achieve the goal with the least amount of
resources. We explore this further in following chapters - a lot of the
time, you’re trying to use less time and less space.
7. Feedback
In beauty, feedback might come from seeing the results on your skin
or getting comments from others. In algorithms, feedback loops help
in refining and improving the algorithm based on the outcomes and
feedback. You can get feedback from professors, managers,
colleagues, and users. It is always important to make sure that these
stakeholders are considered when developing your algorithm.
For example:
Plan the steps - We can approach this with bubble sort, which
involves repeatedly stepping through the list, comparing adjacent
elements, and swapping them if they are in the wrong order. We talk
about this more in later chapters.
5
Documentation - You can document your code by placing
comments in it to explain how each step contributes to our goal.
Loops
6
Conditions
Functions
7
# Example Python code
def find_perfect_outfit(temperature,
occasion):
"""
This function selects the perfect outfit
based on the temperature and the occasion.
It uses conditions to determine the
appropriate outfit.
"""
if occasion == "formal":
outfit += " and skirt"
else:
outfit += " and jeans"
return outfit
outfit =
find_perfect_outfit(temperature, occasion)
print(f"Outfit for {day}: {outfit}")
9
Exercise 1 - Getting Started
1.1 Define an Algorithm:
Given an algorithm that takes a list of numbers and returns the sum,
suggest a way to optimize this algorithm for better performance.
Think about the steps involved and where you might reduce the
number of operations.
Identify a scenario in daily life where algorithms are used (other than
in computing or programming). Explain how algorithmic thinking is
applied in this scenario.
10
Sorting & Searching
In this section, we're going to explore the world of sorting and
searching algorithms. Think of sorting as organizing your wardrobe to
make it both functional and fashionable. It's about putting things in
the right order, from your everyday jeans to your dazzling party
dresses, making it easier to find what you need, when you need it.
11
Bubble Sort
The first sorting algorithm we must explore is Bubble Sort. Now,
Bubble Sort might not be the fastest method out there (think of it as
the leisurely stroll of sorting), but it's a classic and a great way to get
to grips with sorting principles.
The Basics:
Imagine you're going through your collection of party dresses, aiming
to arrange them from the shortest to the longest. You start by
comparing two pairs at a time, swapping their places if the one on the
left is longer than the one on the right. Repeat this comparison and
swap process, and soon, you'll have your dresses lined up perfectly!
This is the fundamental idea of bubble sort.
The Approach:
Start at one end of your array (just like you would start at one end of
your dress rack).
If they're in the wrong order (say, the dress on the left is longer than
the dress on the right), swap them.
12
Repeat this process, moving through the array like you’re shuffling
through your shades, until every item is in its fabulous place.
With each complete pass through the array, the item with the highest
value (in our case, the longest dress) ends up in its correct position.
Here’s why:
In the first pass, the largest element 'bubbles up' to its correct
position at the end of the array.
Bubble Sort is your go-to for smaller, manageable sorting tasks. It's
like choosing an outfit for a casual day out – simple and reliable.
While it may not be the quickest, it's a great starting point for
understanding basic sorting algorithms.
13
Bubble Sort in Action
def bubble_sort(accessory_prices):
n = len(accessory_prices) # Get the
number of items in the array
for i in range(n): # Outer loop
for j in range(0, n-i-1): # Inner
loop to compare adjacent elements
# If current element is greater than
the next, swap them
if accessory_prices[j] >
accessory_prices[j+1]:
accessory_prices[j],
accessory_prices[j+1] = accessory_prices[j+1],
accessory_prices[j] # Swapping
return accessory_prices
# Return the sorted array
We compare each price with the next one and swap them if they're in
the wrong order, and this is how we bubble sort. Remember, we
simply keep comparing and swapping items within little bubbles.
14
Exercise 2 - Bubble Sort
1.1 Bubble Sort in Practice:
Given a list of numbers: [34, 12, 45, 32, 10, 6], apply the Bubble Sort
algorithm to sort the list in ascending order. Write down each step of
the process, showing how you compare and swap elements in the
list, just like you would organize accessories by their style or size.
Consider an already sorted list: [5, 9, 12, 18, 22, 30]. If you apply
Bubble Sort to this list, how many passes will it take to confirm that
the list is already sorted? Explain your answer by describing what
happens during each pass through the list.
14
You can read the rest of this textbook
here.