ma'am audrey's lessons

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:

  1. define data and data structure, and sort the six main structures of this course into linear and non-linear;
  2. define an algorithm, name its five traits, and tell an algorithm apart from a program;
  3. 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.

Recent apps= a stacklast opened, on topChatNotesCameraMusicCanteen line= a queueCOUNTERserved firstjoins at thebackFolders= a treeDocumentsITC 120Photos= a graphFacebook friendsAnaBenCyDanEva
Four things you use every day, and the data structure each one is.

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.

DataData structureAna85Cruz92Ben88Class listNameScoreAna92Ben85Cruz88Loose facts, no order.Which score is Ana's?Arranged in rows:Ana's score is beside her.
Same facts, two ways. Loose slips are data. The class list is a data structure: it arranges the facts so you can find them.

Inside a computer, it works the same way. Here are six scores before and after we put them in an :

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.

Bad fit: one big boxGood fit: a labeled racksalt?✗ Dig through everythingjust to find saltChiliLaurelPepperSaltSugarGarlic✓ Grab the saltin seconds
Same spices, two containers. The labeled rack is the better "data structure" for finding salt.

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:

Data structuresPrimitiveone value eachNon-primitivemany valueswhole numbersbyteshortintlongdecimalsfloatdoubletrue or falsebooleanone charactercharLinearNon-lineararraystackqueuelinked listtreegraphThis course: thenon-primitive ones
Two families first; then the non-primitive family has two kinds. This course is about the non-primitive side.

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:

LinearNon-linear
Arrangementone after another, in a sequencebranching (hierarchy) or a network
Visiting all itemsone natural order (first to last), so one pass visits everythingno single natural order: you must choose a route, and later lessons teach several (pre-order, BFS, DFS)
Connectionseach item links to at most one "next"an item can connect to many others
Examplesarray, stack, queue, linked listtree, graph
Linear: one after another19342756firstlastOne natural order: start at the first, end at the last.Non-linear: branches or a networkABCDEPQRSUtree: branchesgraph: a networkNo single order: you choose a route to visit all.
Linear: one row, first to last. Non-linear: branches (a tree) or a web (a 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

1903142273

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 . Trade-off: fixed size, and inserting in the middle means shifting everything after it.

Linear

Stack

C ← topBA

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

front123rear

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→B→C→null

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

DBEAC

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

ABCD

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

  1. A stack of exam papers waiting to be checked, checked from the top down.

  2. The comment thread on a Facebook post, where replies branch under other replies.

  3. Students lining up to enter the gym for a school event.

  4. The folder structure on your computer's desktop.

  5. Your Facebook friends, where anyone can be friends with anyone.

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).

Input3 cups rice4 garlic cloves2 tbsp oil1 tsp saltSinangag (garlic fried rice)1. Mince the garlic.2. Fry it in the oil until golden.3. Add the rice. Stir for 5 minutes.4. Add the salt. Stir. Done!Exact steps, in order, and it ends.Output:sinangag!
A recipe for sinangag is an algorithm: input, exact steps in order, an end, and an 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:

  1. Input: takes zero or more values to work with.
  2. Output: produces at least one result related to the goal.
  3. Definiteness: each step is clear and unambiguous, with no guesswork.
  4. Finiteness: it stops after a limited number of steps.
  5. 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:

1Inputscores go in9275884Finitenessit stopsSTOPAlgorithm: find the highest score• highest = first score• next score > highest?yes: keep it• no scores left? stop3Definitenessexact, clear steps5Effectivenesssimple steps922Output92 comes out
The find-the-highest-score algorithm as a machine. Every part matches one of the five traits.

To see the traits at work, here are three sets of "steps" that each break one trait:

StepsBreaks this traitWhy
"If the score is high, print Passed."DefinitenessHow high is "high"? A computer needs an exact rule, like score >= 75.
"Print every even number: 2, 4, 6, …"FinitenessThere is no last even number, so it never stops.
"The class list is empty, but find the average anyway: total ÷ 0 students."EffectivenessDividing 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:

    AlgorithmThe steps for solving a problem: 1. ask for two numbers; 2. add them; 3. show the result.
    ProgramCode in a programming language that a computer can run to carry out those steps.
    CodingWriting the instructions in a programming language.
    ProgrammingThe whole process: define the problem → plan → write code → test → debug → improve.
    DefinePlanCodeTestDebugImproveProgramming= the whole loopCoding= one step
    Programming is the whole loop. Coding is one step of it.

    Here is the algorithm from the table (ask for two numbers, add them, show the result) next to its Java program:

    Algorithm: the steps, in words1. Ask for two numbers.2. Add them.3. Show the result.written in JavaProgram: code a computer can runint a = in.nextInt();1int b = in.nextInt();1int sum = a + b;2System.out.println(sum);3
    The numbers on the right show which step each line carries out.

    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 ( ). An algorithm is a list of steps for doing a task ( ). 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.

    Data structurestores the data1934270123Algorithmsteps that work on adata structure, such asfinding the largest✓✓✓✓193427largest: 42Both: one lesson, two labelsData structureAlgorithme.g. Arrays193427the structure+accesstraverseinsertdeletethe steps that use itAbstract data type (ADT)says WHAT it can do,not HOW it is storedA set can:add · removecontainsTechniquea way to build manyalgorithms, such asrecursioncount(n)calls itself
    The four labels you will see under each lesson title, and how one lesson can get two.

    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 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
    LessonTeaches
    Arrays: the four operationsData structureAlgorithm
    Linked listsData structureAlgorithm
    StacksData structure
    QueuesData structure
    Trees and binary treesData structure
    Binary search trees and BST searchData structureAlgorithm
    BST insertionAlgorithm
    Tree traversalsAlgorithm
    Recursion and traversal codeTechnique
    BST deletionAlgorithm
    AVL treesData structureAlgorithm
    Red-black treesData structureAlgorithm
    Graphs, and how to store themData structure
    Breadth-first search (BFS)Algorithm
    Depth-first search (DFS)Algorithm
    Sets in JavaAbstract data type (ADT)
    Maps and hashing in JavaAbstract data type (ADT)Technique
    Linear and binary searchAlgorithm
    Basic sortingAlgorithm
    Advanced sortingAlgorithm
    Hashing and hash tablesTechniqueData structure

    Part 3 · Check yourself

    Self-check

    Not graded. Tap an answer to see if you got it, and read why.

    1. What is a data structure?

    2. Which of these are all linear data structures?

    3. What is an algorithm?

    4. An algorithm must stop after a limited number of steps. Which trait is this?

    5. Which one is a program, not just an algorithm?

    6. Binary search guesses the middle and throws away half. When does this work?

    Score: 0 / 6

    Key takeaways

    1. A data structure stores and arranges data so it can be accessed, searched, and updated efficiently.
    2. Linear: array, stack, queue, linked list. Non-linear: tree, graph.
    3. An algorithm is a finite, well-defined sequence of steps for solving a problem.
    4. Every algorithm needs input, output, definiteness, finiteness, and effectiveness.
    5. 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

    1. A. Bhargava, Grokking Algorithms. Manning, 2016, Chapter 1 (binary search).
    2. D. E. Knuth, The Art of Computer Programming, Vol. 1, 3rd ed. Addison-Wesley, 1997, Section 1.1 (the five traits of an algorithm).
    3. Programiz, "Learn Data Structures and Algorithms" and "Data Structure and Types." programiz.com/dsa
    4. ITC 120 Data Structures and Algorithms Syllabus, Faculty of Computing, Engineering, and Technology, Davao Oriental State University.