Compression

💻 Computer Science Years 10–11 Higher

Why files are compressed, the difference between lossy and lossless, and how run-length encoding and Huffman coding work.

Open this lesson, with practice and a printable worksheet →

Files can be huge, and networks and disks are finite. Compression makes a file smaller so it downloads faster and takes less space. The clever part is doing this without throwing away anything you need — or, when you can afford it, throwing away only what you'll never miss. There are two families: lossless and lossy.

Why this matters: Every streamed video, sent photo and zipped attachment relies on compression. Choosing the right kind — keeping every bit of a legal document, but happily shrinking a holiday snap — is a real decision, and getting it wrong either wastes space or ruins data.
A A A A A A → 6 A count, then value
Run-length encoding: replace a run of repeats with a count and a value.

Lossless compression

originalsmallerexactcopycompressrestoreevery bit comes back
Lossless: the original is restored exactly, bit for bit.

Lossless compression shrinks a file so it can be restored exactly — every original bit comes back. It works by spotting and removing redundancy (repetition), like writing "×6" instead of a word six times. Perfect for text, spreadsheets and program code, where a single changed character matters. Formats: PNG, FLAC, ZIP.

Lossy compression

originaltinyapprox.copydiscard datarestorediscarded data cannot return
Lossy: much smaller, but discarded data is gone for good.

Lossy compression goes further by permanently discarding data the human eye or ear is unlikely to notice — subtle colour shades, or sounds masked by louder ones. The file is much smaller but cannot be perfectly restored. Ideal for photos, music and video, where "good enough" is fine. Formats: JPEG, MP3, MP4.

LosslessLossy
Original restored?ExactlyNo — data lost
File sizeSmallerMuch smaller
Best fortext, codephotos, audio, video
ExamplesPNG, FLAC, ZIPJPEG, MP3, MP4

Run-length encoding (RLE)

RLE is a simple lossless method: a run of identical values is replaced by one copy of the value plus a count. It shines on data with long repeats, such as plain-coloured image regions.

Worked example — apply RLE to AAAAAABBBBCC.
Six A's, four B's, two C's. Encoded as pairs (count, value): 6A 4B 2C.
The original is 12 characters; the code stores three count-value pairs. Decoding is exact, so it is lossless.

Huffman coding — the idea

Huffman coding is a cleverer lossless method. Instead of giving every character the same number of bits, it gives the most frequent characters the shortest codes and rare ones longer codes — like using a quick nickname for the friend you mention most. Overall the message needs fewer bits, with no information lost.

Common mistake: thinking RLE always shrinks a file. On data with few repeats, such as ABCDEF, RLE gives 1A1B1C1D1E1F — larger than the original. Compression only helps when the pattern suits the method.
ABCDEF→1A1B1C1D1E1F6 characters12 characters (bigger!)RLE only helps when there are long runs of repeats.
No repeats: RLE makes ABCDEF larger, not smaller.

Tip: match the method to the data. If losing anything is unacceptable (text, code, medical scans) choose lossless; if the goal is the smallest possible file and slight quality loss is fine (streaming, web photos) choose lossy.

Get the printable worksheet →
← Sound → Inside the CPU
← Sasta Basta home All subjects