Skip to content

Sorting algorithms

Sorting Algorithms

Sorting is the process of arranging items in a specific order (usually ascending or descending).

Common Sorting Strategies

  1. Bubble Sort: Repeatedly swap adjacent elements if they are in the wrong order. Small values "bubble" to the top.
  2. Selection Sort: Find the smallest element and move it to the beginning. Repeat for the rest.
  3. Insertion Sort: Build the sorted list one item at a time by inserting each new item into its correct position.
  4. Merge Sort: Divide the list into halves, sort each half, and then merge them back together. (Fast and reliable).
  5. Quick Sort: Pick a "pivot" element and partition the list around it. (Very fast in practice).

Complexity

In computer science, we measure how "expensive" an algorithm is using Big O Notation. Simple sorts (Bubble, Selection) are usually \(O(n^2)\), while advanced sorts (Merge, Quick) are \(O(n \log n)\).