ITC 120 › Prelim › Introduction
Introduction · Lesson 2 of 2
Introduction to Data Structures and Algorithms
How we store data, and how we tell the computer what to do with it.
By the end, you can:
- define data and data structure, and sort the six main structures of this course into linear and non-linear;
- define an algorithm, name its five traits, and tell an algorithm apart from a program;
- explain, using the guessing game, why the choice of algorithm matters.
Java rusty? Do the Programming refresher first. You need: scratch paper.
Part 1 · Data structures
From raw data to organized data
You are already a data structures expert
Your phone's recent apps (the last one opened is the easiest to get back to). The canteen line (first in line, first served). Folders inside folders in your file manager. Your Facebook friends, linked in a web (your friends have friends, and some of them know each other). Each of these is a data structure in disguise: a stack, a queue, a tree, and a graph.
Data
Raw facts and figures, such as numbers, names, and dates. They have no structure yet.
Data structure
A specific way of storing and arranging data in a computer's memory so it can be accessed, searched, and updated efficiently.
Inside a computer, it works the same way. Here are six scores before and after we put them in an array :
Data (a pile)
→
Data structure (an array)
Same six scores, but now each one has a known position. The structure decides how we can reach and work with the data.
Why it matters: think of your kitchen
Dump every spice into one big box, and you dig through everything just to find salt. Put them on a labeled spice rack, and you grab what you need in seconds. In programming, the "container" is the data structure. The right one makes your program faster and saves memory.
Primitive and non-primitive; linear and non-linear
Books sort data structures in two steps. First, into two families: primitive (one value each, like an int) and non-primitive (many values). Then the non-primitive family splits into two kinds: linear and non-linear. Here is the whole family tree:
Good to know: Java has exactly eight primitive types (all shown above; char holds one character, like 'A'), and Java itself calls them primitive types, not data structures. Some books list them as "primitive data structures" because they are the simplest building blocks. Either way, they hold one value each.
Here is how the two kinds of non-primitive structures compare:
| Linear | Non-linear | |
|---|---|---|
| Arrangement | one after another, in a sequence | branching (hierarchy) or a network |
| Visiting all items | one natural order (first to last), so one pass visits everything | no single natural order: you must choose a route, and later lessons teach several (pre-order, BFS, DFS) |
| Connections | each item links to at most one "next" | an item can connect to many others |
| Examples | array, stack, queue, linked list | tree, graph |
The six main structures of this course
Here are the six structures you will learn, each with a real-life picture. Tap a name to open its lesson:
Linear
Array
Numbered slots, side by side. Like a piso-net shop: a fixed row of numbered units, one user per PC. All slots hold the same type. Strength: instant access by index . Trade-off: fixed size, and inserting in the middle means shifting everything after it.
Linear
Stack
Add (push) and remove (pop) at the top only: last in, first out (LIFO). Like a pile of lunch trays. Used in Undo and the Back button.
Linear
Queue
Join at the rear (enqueue), leave from the front (dequeue): first in, first out (FIFO). Like the canteen line. Used for print jobs.
Linear
Linked list
A chain of nodes. A node is one box that stores a value plus a link to the next node; null means "no next." Like a train: each car is connected to the car behind it. Strength: adding or removing only changes the links around that spot. Trade-off: to reach the 50th node, you follow the links through the 49 before it.
Non-linear
Tree
One top node (the root), branching into children, then grandchildren. Like folders inside folders, a family tree, or a school org chart.
Non-linear
Graph
Nodes (called vertices) joined by lines (called edges). Any node can connect to any other, and there is no single root. Like your friends network, a map app's roads, or web pages linking to each other.
Quick check: classify each one
-
A stack of exam papers waiting to be checked, checked from the top down.
The paper on top (the last one added) is checked first: last in, first out. A stack, which is linear.
-
The comment thread on a Facebook post, where replies branch under other replies.
Each reply belongs to one comment above it, and replies branch out: a tree, which is non-linear.
-
Students lining up to enter the gym for a school event.
The first student in line enters first: first in, first out. A queue.
-
The folder structure on your computer's desktop.
Folders inside folders inside folders: a tree.
-
Your Facebook friends, where anyone can be friends with anyone.
There is no single top, and connections can go in any direction: a graph.
Score: 0 / 5
Part 2 · Algorithms
What is an algorithm?
A data structure only stores things. We still need instructions to search, sort, add, and remove. That is where algorithms come in.
Algorithm
An algorithm is a finite, well-defined sequence of steps for accomplishing a specific task or solving a problem.
Think of a recipe
You gather ingredients (input), follow the steps in order, and each step is exact ("2 tablespoons of oil", not "some oil"). Cooking eventually ends, and you get a finished dish (output).
In programming, "find the largest number in this list" and "sort these student records by grade" are both problems solved by algorithms. Every app on your phone is built from algorithms working on data structures.
Five traits every algorithm has
An algorithm must have all five of these traits. Miss one, and it is just a vague idea:
- Input: takes zero or more values to work with.
- Output: produces at least one result related to the goal.
- Definiteness: each step is clear and unambiguous, with no guesswork.
- Finiteness: it stops after a limited number of steps.
- Effectiveness: each step is simple enough to actually be carried out.
Picture an algorithm as a machine. This one finds the highest score: it looks at each score, keeps the highest one seen so far, and shows it at the end. Each part of the machine shows one trait:
To see the traits at work, here are three sets of "steps" that each break one trait:
| Steps | Breaks this trait | Why |
|---|---|---|
| "If the score is high, print Passed." | Definiteness | How high is "high"? A computer needs an exact rule, like score >= 75. |
| "Print every even number: 2, 4, 6, …" | Finiteness | There is no last even number, so it never stops. |
| "The class list is empty, but find the average anyway: total ÷ 0 students." | Effectiveness | Dividing by zero has no answer, so the computer cannot carry out this step (with whole numbers, Java stops with an error). |
Not all algorithms are equal
Many algorithms can solve the same problem, and some are much faster. Try it: both players below hunt for the same secret number.
Two algorithms, same problem
Pick a secret number from 1 to 100. Both algorithms try to find it. Who needs fewer guesses?
Simple search
Guess 1, then 2, then 3… one at a time.
guesses
Binary search
Guess the middle. "Too low" or "too high" rules out half. Repeat.
guesses
ruled out by this guess still possible ruled out earlier
Worst case for 1 to 100: simple search needs up to 100 guesses; binary search never needs more than 7.
Why only 7? Each "too low" or "too high" throws away about half of what is left. In the worst case, the numbers still possible go 100 → 50 → 25 → 12 → 6 → 3 → 1, so by the 7th guess only one number is left. Simple search may need all 100.
Good to know: binary search only works because the numbers 1 to 100 are in order. "Too low" can rule out the whole lower half only when the values are sorted. You will write it in Java in the Searching lesson, and the same halving idea powers BST search.
Both algorithms solve the same problem, but one is far faster. That is the whole point of this course: data structures + algorithms = programs that run fast and use memory wisely.
Algorithm vs. program, coding vs. programming
These four words are easy to mix up. Here is what each one means:
| Algorithm | The steps for solving a problem: 1. ask for two numbers; 2. add them; 3. show the result. |
|---|---|
| Program | Code in a programming language that a computer can run to carry out those steps. |
| Coding | Writing the instructions in a programming language. |
| Programming | The whole process: define the problem → plan → write code → test → debug → improve. |
Here is the algorithm from the table (ask for two numbers, add them, show the result) next to its Java program:
One algorithm can become many programs. Written in Python or C, the code would look different (a different program), but the three steps would be the same (the same algorithm).
Optional: see it as a complete Java program
For the curious only; you do not need to memorize this. Scanner reads what the user types. The first line, import java.util.Scanner;, tells Java where to find Scanner. The class and main around the steps are explained in block 8 of the Programming refresher.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: "); // step 1
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
int sum = a + b; // step 2
System.out.println(sum); // step 3
}
} ▶ This program runs as is. Try it in the Programiz online compiler: tap Copy, paste it there, and press Run.
In the online compiler, type the two numbers in the input area when the program asks. With 4 and 5, it prints 9.
Your coding task: from steps to Java
Now turn an algorithm into a program yourself. The comments at the top list three steps that decide a student's result. Each step is exact (definiteness): "75 or higher" leaves no doubt. Finish result by writing one Java check for each step, about 7 lines, where it says YOUR CODE. Then run it: the five printed words must match the comments. You need if, else if, ||, and return, from blocks 3 and 5 of the Programming refresher.
1public class Main {
2 // The algorithm, step by step:
3 // 1. If the score is below 0 or above 100, the answer is "Invalid".
4 // 2. Otherwise, if the score is 75 or higher, the answer is "Passed".
5 // 3. Otherwise, the answer is "Failed".
6 static String result(int score) {
7 // YOUR CODE: turn steps 1 to 3 into Java.
8
9 return "";
10 }
11
12 public static void main(String[] args) {
13 System.out.println(result(88)); // expected: Passed
14 System.out.println(result(75)); // expected: Passed
15 System.out.println(result(74)); // expected: Failed
16 System.out.println(result(-5)); // expected: Invalid
17 System.out.println(result(101)); // expected: Invalid
18 }
19} ▶ This program runs as is. Try it in the Programiz online compiler: tap Copy, paste it there, and press Run.
Show one possible answer
1static String result(int score) {
2 if (score < 0 || score > 100) {
3 return "Invalid";
4 } else if (score >= 75) {
5 return "Passed";
6 } else {
7 return "Failed";
8 }
9} Output: Passed, Passed, Failed, Invalid, Invalid. Step 1 must be checked first: if score >= 75 came first, 101 would print Passed. The test with 75 checks the exact edge of the rule: score > 75 would wrongly print Failed for it. Trying the edges like this is the "test" step of programming.
Data structure, algorithm, or both?
This course is called Data Structures and Algorithms, and its lessons teach both. A data structure is a way to store and organize data (see Part 1 ). An algorithm is a list of steps for doing a task (see above ). Many lessons teach both at once, so they show two labels under the title: a data structure, plus the algorithms that work on it (search it, add to it, remove from it). Arrays is one of them.
Two more words appear in later lessons:
- An abstract data type (ADT) says what a collection can do (its operations), not how it is stored. Sets and maps are ADTs; a hash table is one data structure that can store them (you will meet sets, maps, and hash tables near the end of the course).
- A technique is a way of building many algorithms, like recursion (a method that calls itself) or hashing (also near the end of the course).
Every lesson shows these labels under its title. Here is the whole course at a glance:
Show every lesson and its labels
| Lesson | Teaches |
|---|---|
| Arrays: the four operations | Data structureAlgorithm |
| Linked lists | Data structureAlgorithm |
| Stacks | Data structure |
| Queues | Data structure |
| Trees and binary trees | Data structure |
| Binary search trees and BST search | Data structureAlgorithm |
| BST insertion | Algorithm |
| Tree traversals | Algorithm |
| Recursion and traversal code | Technique |
| BST deletion | Algorithm |
| AVL trees | Data structureAlgorithm |
| Red-black trees | Data structureAlgorithm |
| Graphs, and how to store them | Data structure |
| Breadth-first search (BFS) | Algorithm |
| Depth-first search (DFS) | Algorithm |
| Sets in Java | Abstract data type (ADT) |
| Maps and hashing in Java | Abstract data type (ADT)Technique |
| Linear and binary search | Algorithm |
| Basic sorting | Algorithm |
| Advanced sorting | Algorithm |
| Hashing and hash tables | TechniqueData structure |
Part 3 · Check yourself
Self-check
Not graded. Tap an answer to see if you got it, and read why.
-
What is a data structure?
Data is the raw facts; a data structure is how we organize them. The last option describes an algorithm.
-
Which of these are all linear data structures?
Linear structures arrange items one after another. Trees and graphs branch out, so they are non-linear.
-
What is an algorithm?
An algorithm is the steps; a program is code that carries them out.
-
An algorithm must stop after a limited number of steps. Which trait is this?
Finiteness = it ends. Definiteness = each step is clear; effectiveness = each step can actually be done.
-
Which one is a program, not just an algorithm?
The other three are steps (algorithms). Java code that a computer can run is a program.
-
Binary search guesses the middle and throws away half. When does this work?
"Too low" can rule out the whole lower half only because the values are in order. On an unsorted list, the secret could be anywhere.
Score: 0 / 6
Key takeaways
- A data structure stores and arranges data so it can be accessed, searched, and updated efficiently.
- Linear: array, stack, queue, linked list. Non-linear: tree, graph.
- An algorithm is a finite, well-defined sequence of steps for solving a problem.
- Every algorithm needs input, output, definiteness, finiteness, and effectiveness.
- The choice of structure and algorithm affects how fast and efficient a program is (binary search: at most 7 guesses for 1 to 100; simple search: up to 100).
Next lesson: Arrays.
References
- A. Bhargava, Grokking Algorithms. Manning, 2016, Chapter 1 (binary search).
- D. E. Knuth, The Art of Computer Programming, Vol. 1, 3rd ed. Addison-Wesley, 1997, Section 1.1 (the five traits of an algorithm).
- Programiz, "Learn Data Structures and Algorithms" and "Data Structure and Types." programiz.com/dsa
- ITC 120 Data Structures and Algorithms Syllabus, Faculty of Computing, Engineering, and Technology, Davao Oriental State University.