What are data structures and algorithms?

Data structures organize data, algorithms describe step-by-step procedures to solve problems. Learn the basics with clear examples.

TwiddleLabs··5 min read

Data structures are ways to store collections of items. Algorithms are step-by-step instructions that work on those items to solve a problem. Together they let a computer turn raw input into useful output.

How they work together

A data structure decides how data is arranged in memory. An algorithm decides how to move through that arrangement. For example, a list holds numbers in a specific order. A sorting algorithm looks at the numbers, compares them, and rearranges them until the list is ordered from smallest to largest. The list is the container; the algorithm is the process that changes the container.

A concrete example

Imagine you have five cards numbered 3, 1, 4, 2, 5. You want them in ascending order. One simple algorithm is insertion sort.

  1. Start with the first card (3). It is already in a sorted part.
  2. Take the next card (1). Compare it with the sorted part. 1 is smaller than 3, so move 3 right and place 1 at the beginning. The order is now 1, 3, 4, 2, 5.
  3. Take the next card (4). Compare with 3; 4 is larger, so it stays where it is. Order: 1, 3, 4, 2, 5.
  4. Take the next card (2). Compare with 4 (larger) – move 4 right. Compare with 3 (larger) – move 3 right. Compare with 1 (smaller) – stop. Insert 2 after 1. Order: 1, 2, 3, 4, 5.
  5. Take the last card (5). It is larger than all sorted cards, so it stays. Final order: 1, 2, 3, 4, 5.

The cards are the data structure (a simple list). The steps you followed are the algorithm. The algorithm repeatedly accesses the list, compares values, and moves items until the list meets the goal of being sorted.

Why they matter

Without a clear structure, a program would have to search through a jumble of values each time it needed information, which is slow and error-prone. Without a clear algorithm, even the best-organized data would sit unused. Good pairings make programs fast, reliable, and easier to understand.

Common data structures

Structure Typical use
Array Fixed-size, random access, e.g., storing a month’s temperatures
Linked list Inserting or deleting items frequently, e.g., a music playlist
Stack Last-in-first-out tasks, e.g., undo history
Queue First-in-first-out ordering, e.g., printer jobs
Tree Hierarchical data, e.g., file system directories
Graph Networks of connections, e.g., social-network friends

Each structure has strengths and weaknesses. Choosing the right one depends on the operations you need most often.

Common algorithm categories

  • Search – locate a target value (binary search, linear search).
  • Sort – arrange items in order (insertion sort, quicksort).
  • Traversal – visit every element, often in a specific order (depth-first search on a tree).
  • Recursion – solve a problem by calling the same procedure on a smaller sub-problem (factorial, Fibonacci).

Choosing the right pair

Suppose you need to find a student’s record by ID. If the records are stored in an unsorted array, you would have to look at each entry until you find a match – a linear search that takes time proportional to the number of records. If you first sort the array (using a sorting algorithm) and then apply binary search, each lookup takes only logarithmic time. The sorting step is an algorithm that prepares the data structure (the array) for a faster search algorithm.

More textbook examples

  • Binary search – works on a sorted array. The algorithm repeatedly halves the interval, discarding the half that cannot contain the target. Textbooks often illustrate this with a phone book sorted alphabetically.
  • Queue for printer jobs – a queue stores print requests in the order they arrive. The processing algorithm removes the front request, prints it, and repeats until the queue is empty.
  • Stack for expression evaluation – when evaluating a mathematical expression in postfix notation, a stack holds intermediate numbers. The algorithm pushes numbers, pops them when an operator appears, computes the result, and pushes it back.
  • Dijkstra’s shortest-path algorithm – uses a priority queue (a specialized data structure) to repeatedly select the next closest vertex. The algorithm updates distances and builds the shortest-path tree.

These examples show how a structure (array, queue, stack, priority queue) and an algorithm (binary search, dequeue-process, postfix evaluation, Dijkstra) combine to solve concrete problems.

Common beginner mistakes

  • Mixing up data and process. New learners often think a data structure does the work itself. It only holds data; the algorithm does the work.
  • Choosing the wrong structure. Trying to search a random list with binary search will fail because binary search requires the list to be sorted.
  • Ignoring edge cases. Forgetting what happens with an empty list or a list with one element leads to errors in both the structure and the algorithm.
  • Assuming constant speed. Many beginners expect every operation to take the same amount of time. Accessing the middle of a linked list, for instance, is slower than accessing an array index.
  • Overlooking complexity. Students sometimes pick a simple algorithm without checking how its running time grows with input size, leading to slow programs on large data.
  • Off-by-one errors. When looping over indices, counting from 1 instead of 0 (or vice-versa) can cause missed elements or out-of-range crashes.

Check yourself

  1. Question: What two parts are needed to turn an unsorted list of numbers into a sorted list? Answer: A list (data structure) and a sorting algorithm.
  2. Question: Why does binary search require the list to be sorted? Answer: Because it decides which half to discard based on order; without order the decision would be meaningless.
  3. Question: If you add items to the front of a linked list, what operation becomes fast? Answer: Insertion at the front, because it only changes a few pointers.

For a deeper look at these concepts, see the Ordo page where they are explored in more detail.

← All posts