Overview
Selection Sort divides the array into two parts: a sorted portion at the beginning and an unsorted portion at the end. It repeatedly selects the smallest (or largest) element from the unsorted portion and swaps it into the correct position in the sorted portion.Complexity
- Time Complexity: O(n²) in all cases (best, average, and worst)
- Space Complexity: O(1) - sorts in-place
- Stable: No - may change relative order of equal elements (though stable variants exist)
Implementation
Here’s the actual implementation from the Sorting Algorithms Visualiser:How It Works
- Outer Loop: Iterates from position 0 to n-2 (position
imarks the boundary of the sorted portion) - Initialize minimum: Assume
minId = i(current position has the minimum value) - Inner Loop: Search through unsorted portion (from
i+1to end)- If a smaller element is found, update
minId = j
- If a smaller element is found, update
- Swap: After finding the actual minimum, swap it with element at position
i - Repeat: The sorted portion grows by one element each iteration
- Verification: After sorting completes, highlight each element to confirm sorted order
Step-by-Step Example
For the array[64, 25, 12, 22, 11]:
Pass 1 (i=0):
- Find minimum in
[64, 25, 12, 22, 11]→11at index 4 - Swap 64 and 11 →
[11, 25, 12, 22, 64]
- Find minimum in
[25, 12, 22, 64]→12at index 2 - Swap 25 and 12 →
[11, 12, 25, 22, 64]
- Find minimum in
[25, 22, 64]→22at index 3 - Swap 25 and 22 →
[11, 12, 22, 25, 64]
- Find minimum in
[25, 64]→25at index 3 - No swap needed →
[11, 12, 22, 25, 64]
Watch It in Action
When running the visualiser, you’ll see:- Red highlight (highlight1): Current element being examined
j(when it’s a new minimum) - Green highlight (highlight2): The boundary position
iwhere the minimum will be placed - Blue highlight (highlight3): Current element being examined
j(when it’s not a new minimum)
The color highlighting differentiates between finding a new minimum element (red) versus just scanning through elements (blue), making it easy to see when the minimum changes.
Best Use Cases
- Small datasets: Acceptable for small arrays where simplicity matters
- Memory-constrained systems: Requires only O(1) additional space
- Minimizing swaps: Makes at most O(n) swaps (useful when writing to memory is expensive)
- Educational purposes: Simple to understand and teach
Advantages
✓ Simple to understand and implement ✓ Performs well with small datasets ✓ Minimizes number of swaps - at most n swaps (useful when swap operation is expensive) ✓ In-place sorting (O(1) space) ✓ Performs same number of comparisons regardless of inputDisadvantages
✗ O(n²) time complexity in all cases - even if already sorted ✗ Not stable (may change relative order of equal elements) ✗ Does not adapt to partially sorted data ✗ Performs many unnecessary comparisons ✗ Generally slower than Insertion Sort for small arraysComparison with Other Sorts
| Feature | Selection Sort | Insertion Sort | Bubble Sort |
|---|---|---|---|
| Time Complexity | Always O(n²) | O(n) to O(n²) | O(n) to O(n²) |
| Swaps | O(n) | O(n²) | O(n²) |
| Stable | No | Yes | Yes |
| Adaptive | No | Yes | Can be |
| Best for | Minimizing swaps | Nearly sorted data | Teaching |