Introduction to Data Structures

What data structures and algorithms are, the main kinds, and why the choice matters.

A data structure is a way of organising data in memory so that the operations you need (search, insert, delete, find the smallest, ...) are fast. An algorithm is a finite sequence of well-defined steps that solves a problem. The two go together: a good algorithm is usually a good idea about how to arrange the data.

Data structuresLinearitems in a sequenceArrayLinked listStackQueueNon-linearitems in a hierarchy or networkTreeHeapGraphHash-baseditems found by keyHash table
The main families of data structures covered in this track
  • Linear structures store items one after another; each item has at most one predecessor and one successor.
  • Non-linear structures let an item connect to several others, like a family tree or a road map.
  • Hash-based structures jump straight to an item computed from its key.

Why the choice matters

Suppose you need to check whether a number is in a collection of 1000 sorted numbers. Checking one by one (linear search) can take 1000 comparisons. Because the data is sorted, you can instead look at the middle and throw away half each time (binary search), which needs at most 10 comparisons. The data is the same; the algorithm makes it 100 times faster.

public class Main {
    static int linearSteps(int[] a, int target) {
        int steps = 0;
        for (int x : a) {
            steps++;
            if (x == target) break;
        }
        return steps;
    }

    static int binarySteps(int[] a, int target) {
        int lo = 0, hi = a.length - 1, steps = 0;
        while (lo <= hi) {
            steps++;
            int mid = (lo + hi) / 2;
            if (a[mid] == target) break;
            if (a[mid] < target) lo = mid + 1; else hi = mid - 1;
        }
        return steps;
    }

    public static void main(String[] args) {
        int[] nums = new int[1000];
        for (int i = 0; i < nums.length; i++) nums[i] = i + 1;   // 1, 2, ..., 1000
        System.out.println("Linear search for 999: " + linearSteps(nums, 999) + " comparisons");
        System.out.println("Binary search for 999: " + binarySteps(nums, 999) + " comparisons");
    }
}
Output
Linear search for 999: 999 comparisons
Binary search for 999: 9 comparisons

Abstract data types

An abstract data type (ADT) describes what operations are allowed, not how they are implemented. A stack ADT promises push, pop and peek; it could be built on an array or on a linked list. Separating the "what" from the "how" lets you swap implementations without changing the code that uses them.

ADTMain operationsCommon implementations
Listget, insert, delete by positionarray, linked list
Stackpush, pop, peek (last in, first out)array, linked list
Queueenqueue, dequeue (first in, first out)circular array, linked list
Map / dictionaryput, get, remove by keyhash table, balanced search tree
Priority queueinsert, remove the minimumbinary heap

GATE CS syllabus map

This track covers the GATE Computer Science syllabus for Programming and Data Structures and Algorithms. (Programming in C itself is taught in the C tutorial.)

GATE syllabus topicLessons
RecursionRecursion and Recurrence Relations
Asymptotic worst-case time and space complexityTime and Space Complexity, Recurrence Relations
ArraysArrays and Dynamic Arrays
Stacks and queuesStacks, Queues and Deques
Linked listsLinked Lists
Trees and binary search treesBinary Trees, Binary Search Trees, AVL Trees
Binary heapsHeaps and Priority Queues
Graphs and graph traversalsGraphs, BFS and DFS, DFS Applications
Shortest pathsShortest Paths
Minimum spanning treesMinimum Spanning Trees
SearchingSearching
SortingSimple Sorting, Fast Sorting, Heapsort
HashingHash Tables
Divide and conquerDivide and Conquer
Greedy algorithmsGreedy Algorithms
Dynamic programmingDynamic Programming
Mixed revisionGATE Practice Set

GATE-style practice questions

GATE-styleMCQ · one correct answer

Which of the following is a non-linear data structure?

Show answer and explanationAnswer: C

In a binary tree a node can have two children, so items are arranged in a hierarchy rather than a single sequence. Stacks, queues and arrays are all linear.

GATE-styleMSQ · one or more correct answers

Which of the following are abstract data types rather than concrete implementations? (Select all that apply.)

Show answer and explanationAnswer: A, B

A stack and a priority queue only specify operations. A singly linked list and an array-based binary heap are concrete ways of storing data, and can be used to implement those ADTs.

GATE-styleNAT · numerical answer

Binary search is run on a sorted array of 1000 distinct elements. What is the maximum number of element comparisons (each comparison of the target with a[mid] counts as one) needed to decide whether a value is present?

Show answer and explanationAnswer: 10

Each comparison halves the remaining range. After k comparisons at most 1000 / 2^k elements remain, and the worst case needs floor(log2 1000) + 1 = 9 + 1 = 10 comparisons.