Sorting algorithms
Sorting Algorithms¶
Sorting is the process of arranging items in a specific order (usually ascending or descending).
Common Sorting Strategies¶
- Bubble Sort: Repeatedly swap adjacent elements if they are in the wrong order. Small values "bubble" to the top.
- Selection Sort: Find the smallest element and move it to the beginning. Repeat for the rest.
- Insertion Sort: Build the sorted list one item at a time by inserting each new item into its correct position.
- Merge Sort: Divide the list into halves, sort each half, and then merge them back together. (Fast and reliable).
- 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)\).