Fundamentals 20 min read

Unlock Core Data Structures, Algorithms, and Sorting Basics

This article provides a comprehensive introduction to fundamental data structures, core algorithm concepts, and classic sorting techniques, explaining their definitions, basic operations, time and space complexities, stability considerations, and practical use cases for developers seeking to strengthen their programming foundations.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Unlock Core Data Structures, Algorithms, and Sorting Basics

Introduction

Understanding algorithms and data structures is essential for solving specific problems, optimizing program performance, and translating real‑world challenges into computational solutions.

Why Learn Algorithms and Data Structures?

Solve specific problems.

Lay the foundation for deep performance optimization.

Learn a mindset for modeling real‑world issues in code.

What Level Should Business Developers Master?

Know common data structures and algorithms to communicate without barriers.

Apply the right structure or algorithm when encountering performance‑critical scenarios.

Data Structure Basics

What Is a Data Structure?

A data structure defines how data is organized, managed, and stored to enable efficient access and modification; it serves as the foundation for algorithms.

Physical vs. Logical Structure

Physical structures (e.g., arrays, linked lists) are tangible and directly manipulable, while logical structures (e.g., queues, stacks, trees, graphs) represent abstract relationships.

Linear vs. Non‑Linear Storage

Linear: each element relates one‑to‑one (e.g., stacks, queues).

Non‑linear: elements may connect to multiple others (e.g., trees, graphs).

Algorithm Basics

What Is an Algorithm?

Mathematics: a formula or idea for solving a class of problems.

Computer science: a sequence of instructions that solves a specific computational task.

How to Evaluate an Algorithm?

Time complexity – how execution time grows with input size.

Space complexity – how memory consumption grows with input size.

Calculating Time Complexity

Big‑O notation abstracts the dominant term of the runtime function T(n), ignoring constants and lower‑order terms.

Typical hierarchy: O(1) > O(log n) > O(n) > O(n log n) > O(n²).

Calculating Space Complexity

Constant space O(1) – fixed size independent of input.

Linear space O(n) – proportional to input size.

Quadratic space O(n²) – proportional to the square of input size.

Recursive space O(log n) – stack depth proportional to recursion depth.

Algorithm Stability

Stable: equal keys preserve their original order after sorting. Unstable: equal keys may reorder.

Common Algorithms

String: brute‑force, BM, KMP, Trie.

Search: binary search, traversal search.

Sorting: bubble sort, quick sort, counting sort, heap sort, etc.

Search engines: TF‑IDF, PageRank.

Clustering: EM, k‑means.

Deep learning: DBN, CNN, GAN.

Anomaly detection: k‑NN, LOF.

Common Data Structures

Array

An ordered collection of elements of the same type, supporting O(1) read/write, O(n) insert/delete, and O(n) expansion.

Array illustration
Array illustration

Linked List

A non‑contiguous structure composed of nodes, each containing data and a pointer to the next node.

Operations: read O(n), update O(1), insert O(1), delete O(1).

Linked list illustration
Linked list illustration

Stack

A linear logical structure with LIFO semantics. Implementations can use arrays or linked lists.

Operations: push O(1), pop O(1).

Applications: call stack, breadcrumb navigation.

Stack illustration
Stack illustration

Queue

A linear logical structure with FIFO semantics, implemented via arrays or linked lists.

Operations: enqueue O(1), dequeue O(1).

Applications: message queues, thread waiting queues, URL crawling queues.

Queue illustration
Queue illustration

Hash Table

A logical structure mapping keys to values. Basic operations: write O(1), read O(1), resize O(n). Collisions are resolved by open addressing or chaining.

Hash table illustration
Hash table illustration
index = HashCode("001121") % Array.length = 7
index = HashCode("this") % Array.length = 6

Tree

A finite set of nodes with a single root; each node may have child sub‑trees.

Traversals:

Depth‑first: preorder (root‑left‑right), inorder (left‑root‑right), postorder (left‑right‑root).

Breadth‑first: level‑order.

Preorder traversal
Preorder traversal
Inorder traversal
Inorder traversal
Postorder traversal
Postorder traversal

Binary Tree

A special tree where each node has at most two children.

Full binary tree: every non‑leaf node has two children and all leaves are at the same depth.

Complete binary tree: nodes are filled level by level left to right without gaps.

Complete binary tree
Complete binary tree

Binary Search Tree (BST)

Each node's left subtree contains smaller values, right subtree contains larger values; both subtrees are themselves BSTs.

Uses: binary search, in‑order traversal for sorting.

BST illustration
BST illustration

Binary Heap

A complete binary tree satisfying the heap property: max‑heap (parent ≥ children) or min‑heap (parent ≤ children).

Operations: insert (bubble up), delete (swap root with last element, then sink), heapify.

Binary heap illustration
Binary heap illustration

Common Sorting Algorithms

Bubble Sort

Repeatedly compare adjacent elements and swap if out of order; the largest element “bubbles” to the end each pass.

Complexity: O(n²) time, O(1) space; best for small or nearly sorted datasets.

Bubble sort steps
Bubble sort steps

Merge Sort

A divide‑and‑conquer algorithm that recursively splits the array, sorts each half, and merges them.

Complexity: O(n log n) time, O(n) extra space; stable.

Merge sort process
Merge sort process

Quick Sort

Uses partitioning around a pivot to recursively sort sub‑arrays.

Complexity: average O(n log n), worst‑case O(n²); in‑place but unstable.

Quick sort partition
Quick sort partition

Heap Sort

Builds a max‑heap, repeatedly extracts the maximum, and restores the heap.

Complexity: O(n log n) time, O(1) extra space; not stable.

Heap sort steps
Heap sort steps

Counting Sort

A non‑comparison sort that counts occurrences of each distinct value and reconstructs the sorted array.

Complexity: O(n + k) time, where k is the range of input values; stable but limited to integer keys.

Counting sort process
Counting sort process

Bucket Sort

Distributes elements into a number of buckets based on a mapping function, then sorts each bucket individually.

Complexity: O(n) average time for uniformly distributed data; performance depends on bucket distribution.

Bucket sort illustration
Bucket sort illustration

Performance Comparison

A benchmark sorting random numbers in the range 0 ~ K for N elements shows the relative speed of each algorithm, highlighting that quick sort and merge sort generally outperform O(n²) methods, while counting and bucket sorts excel when the input range is limited.

Sorting algorithm performance chart
Sorting algorithm performance chart
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Data Structuresbinary treeAlgorithmscomplexityhash tableSorting
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.