Chapters 1-18 Overview (all what you need to know) 2025 new update Western
Governors University
A Common-Sense Guide to Data Structures and
Algorithms Study Guide (Chapters 1 to 18)
CHAPTER 1: Why Data Structures Matter
When newcomers start coding, they focus mainly on making their code work. The success of the code is
determined by whether it functions correctly or not. As they advance, software engineers learn to evaluate
their code on more complex levels. They realize that two pieces of code might achieve the same result, but
one could be considered superior.
Code quality is assessed by various factors. A significant aspect is code maintainability, including the
readability, structure, and modularity of the code. But there’s another key quality, code efficiency, which
involves how fast the code runs. Two different code snippets might accomplish the same objective, but one
could execute faster.
Consider two functions that print even numbers from 2 to 100. The first function checks if each number is even
and then prints it, while the second function simply adds 2 to the number and prints it:
def print_numbers_version_one():
number = 2
while number <= 100:
# If number is even, print it:
if number % 2 == 0:
print(number)
, number += 1
def print_numbers_version_two():
number = 2
while number <= 100:
print(number)
# Increase number by 2, which, by definition,
# is the next even number:
number += 2
The second function runs faster because it loops only 50 times compared to 100 times in the first function,
making it more efficient. This illustration highlights the importance of writing efficient code and how
understanding data structures can affect the speed of code, essential skills for becoming an advanced software
developer.
Data Structures
Data refers to all kinds of information, including basic numbers and strings. Even complex data can be broken
down into these simple components. Data structures describe how data is arranged. The same data can be
organized in different ways, and these variations can greatly affect the speed of your code. Consider a simple
example where you have three strings that form a message. You can store them as independent variables:
x = "Hello! "
, y = "How are you "
z = "today?"
print x + y + z
Or you can organize them in an array:
array = ["Hello! ", "How are you ", "today?"]
print array[0] + array[1] + array[2]
The way you choose to structure your data is not just a matter of tidiness. It can influence how quickly your
code runs and even whether it can handle large loads. For instance, if you're building a web app used by many
people at once, the right data structures can prevent it from crashing due to overload.
Understanding how data structures affect the performance of your software empowers you to write efficient
and sophisticated code, enhancing your skills as a software engineer. This chapter will introduce you to the
analysis of two specific data structures: arrays and sets. You'll learn how to examine their performance
implications, even though they might seem quite similar at first glance.
The Array: The Foundational Data Structure
The array is a fundamental data structure, essentially a list of data elements. It's a versatile tool used in various
situations. For example, if you're writing an application for creating grocery shopping lists, you might have an
array like this:
array = ["apples", "bananas", "cucumbers", "dates", "elderberries"]
This specific array has five strings, representing items to buy at the supermarket. When talking about arrays,
some specific terms are often used:
• Size: The number of data elements in the array. The above array has a size of 5 because it has five
values.
• Index: The number that shows the position of a piece of data within the array. In most programming
languages, the index starts at 0. So, in the given example, "apples" is at index 0, and "elderberries" is at
index 4.
Thus, arrays are a foundational structure, used to store and organize data in an accessible and ordered manner.
Data Structure Operations
To gauge the performance of a data structure like an array, we need to look at how code interacts with it. This
is typically done through four basic operations:
, 1. Read: This operation looks up a specific value within the data structure at a particular spot. In an array,
you would find a value at a specific index. For instance, finding which grocery item is at index 2 is a read
operation.
2. Search: This operation seeks a particular value within the data structure. In the context of an array, it
means looking to see if a specific value exists, and if so, at which index. If you were to find the index of
"dates" in a grocery list, you would be searching the array.
3. Insert: This refers to adding a new value to the data structure. In an array, it means placing a new value
in an available slot. Adding "figs" to a shopping list would be an insert operation in the array.
4. Delete: This operation involves removing a value from the data structure. In an array, it means taking
out one of the values. If "bananas" were removed from a grocery list, that value would be deleted from
the array.
Measuring Speed
Measuring the speed of an operation in programming doesn't mean calculating the time it takes in seconds or
minutes. Instead, it refers to the number of computational steps required to complete the operation. Here's
why this approach is used:
1. Inconsistency in Time Measurement: An operation might take five seconds on one computer but could
take more or less time on another machine. Time is an unreliable measure since it varies depending on
the hardware used.
2. Steps as a Universal Measure: By counting the number of computational steps, you can make a
consistent comparison between different operations. If Operation A takes 5 steps and Operation B takes
500 steps, you can always conclude that Operation A will be faster, regardless of the hardware.
3. Terms Used for Measuring Speed: Throughout literature and discussions, you may encounter terms like
speed, time complexity, efficiency, performance, and runtime. They all mean the same thing in this
context: the number of steps an operation takes.
This concept of measuring speed by counting steps is critical for understanding and analyzing the efficiency of
different operations, such as those performed on data structures like an array. It provides a standard way to
evaluate how quickly a piece of code will run, regardless of where it's executed.
Reading
Reading from an array is a quick and straightforward process for a computer. An array is like a list, such as
["apples", "bananas", "cucumbers", "dates", "elderberries"]. If you want to find out what's at a certain
position, like index 2, the computer can instantly tell you that it's "cucumbers." How does this happen? Let's
understand:
1. Computer Memory: Think of computer memory as a huge collection of cells or slots. Some are empty,
while others contain data bits.