Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: RecursionDemo

TimeComplexity

//
// TimeComplexity.java
//


// This Processing program creates the time complexity plot for the
// Algorithms chapter.  
//
// The program uses some modern Java features: 
// - functional interfaces (DoubleUnaryOperator) 
// - lambda expressions (x -> Math.log(x)) 
// - type inference (var)
//
// The program also uses some graphics techniques available
// in Processing:
// - transformations to draw first quadrant [0,10]x[0,10] using the
//   standard math orientation
// - offscreen buffer (PGraphics) to create an image with an alpha
//   channel (transparency), which is then saved as a .png file.
//


import processing.core.*;
import java.util.*;
import java.util.function.*;


public class TimeComplexity extends PApplet
{
    public void settings()
    {
        size(800, 800);
    }

    public void setup()
    {
        pg = drawStuff();
    }

    public void drawGraph(PGraphics pg, DoubleUnaryOperator function)
    {
        pg.stroke(0);
        pg.strokeWeight(.1f);
        pg.noFill();

        pg.beginShape();
        for (float x=1.0f; x<=10.0f; x+=.1f)
        {
            float y = (float) function.applyAsDouble(x);
            pg.vertex(x, y);
        }
        pg.endShape();
    }

    public void drawGraphs(PGraphics pg)
    {
        // transform to show first quadrant [0,10]x[0,10] in math
        // orientation

        pg.pushMatrix();

        final float margin = .1f;
        pg.translate(width*margin, height-height*margin);
        float scalingFactor = (1 - 2*margin)*width/10;
        pg.scale(scalingFactor, -scalingFactor);

        // draw axes

        pg.stroke(0);
        pg.strokeWeight(.2f);
        pg.line(0, 0, 10, 0);
        pg.line(0, 0, 0, 10);

        // draw function graphs

        DoubleUnaryOperator[] functions = {
            x -> Math.log(x),       // logarithmic
            x -> x,                 // linear
            x -> x * Math.log(x),   // log-linear
            x -> x*x,               // quadratic
         };

        for (var f : functions)
            drawGraph(pg, f);

        pg.popMatrix();
    }

    public void drawLabels(PGraphics pg)
    {
        pg.textSize(32); 
        pg.fill(0);
        pg.text("logarithmic\nO(log n)", width*.72f, height*.64f);
        pg.text("linear\nO(n)", width*.85f, height*.21f);
        pg.text("log-linear\nO(n log n)", width*.6f, height*.05f);
        pg.text("quadratic\nO(n*n)", width*.18f, height*.05f);
    }

    public PGraphics drawStuff()
    {
        // draw everything to an offscreen buffer (PGraphics)

        PGraphics pg = createGraphics(width, height);

        pg.beginDraw();
        pg.background(255, 0);
        drawGraphs(pg);
        drawLabels(pg);
        pg.endDraw();

        return pg;
    }

    public void draw()
    {
        background(255);
        image(pg, 0, 0);
    }

    public void keyPressed()
    {
        String filename = "time_complexity.png";
        println("Writing " + filename);
        pg.save(filename);
    }

    private PGraphics pg;

    public static void main(String[] args)
    {
        PApplet.main("TimeComplexity");
    }
}

Next: