Selection Sort Visualizer
Selection Sort is a simple comparison-based sorting algorithm that divides the array into sorted and unsorted portions, repeatedly finding the minimum element from the unsorted portion and placing it at the beginning.Try the live interactive visualizer: Selection Sort Visualizer
How Selection Sort Works
Selection Sort builds the sorted array one element at a time by repeatedly selecting the smallest remaining element.Initialize outer loop
Start from the first element and iterate through the array (stopping one element before the end, since the last element will automatically be in place).
Find minimum element
For each position
i, assume it holds the minimum value (minPos = i). Then scan through all remaining unsorted elements (j = i+1 to end) to find the actual minimum.Update minimum position
When a smaller element is found at position
j, update minPos to j. This tracks the index of the smallest element in the unsorted portion.Swap elements
After completing the inner loop, swap the element at
minPos with the element at i. This places the minimum element in its correct sorted position.Understanding the Visualization
The DS Visualizer implementation provides visual feedback during the sorting process:Color Indicators
- Purple (
#be12c9): Current position being sorted (outer loop positioni) - Cyan (
#12c9bd): Current minimum element found in the unsorted portion - Gray (
#9ca3af): Default/unsorted elements
Animation Behavior
The visualizer uses scale transformations and color changes to highlight comparisons:Algorithm Implementation
Here’s the core sorting logic from the visualizer:Key Implementation Details
- Outer loop runs to
values.length - 1: The last element is automatically in place once all others are sorted - Inner loop starts at
i + 1: Only unsorted elements are compared - Visual delay is proportional to remaining elements:
1000 * (values.length - i)provides longer highlights for earlier iterations - Swap happens after finding minimum: Elements are only swapped once per outer loop iteration
Java Implementation
Here’s a complete Java implementation of Selection Sort:The outer loop terminates at
arr.length - 1 because when all but one element are in their correct positions, the last element is automatically sorted.Complexity Analysis
Time Complexity
- Worst Case: O(n²) - Even if the array is already sorted, Selection Sort still performs all comparisons
- Average Case: O(n²) - The nested loops always execute the same number of times regardless of input
- Best Case: O(n²) - Unlike some algorithms, Selection Sort doesn’t benefit from pre-sorted data
(n-1) + (n-2) + ... + 1 = n(n-1)/2 comparisons.
Space Complexity
- O(1) - Selection Sort is an in-place sorting algorithm that only uses a constant amount of extra space for the
minPosandtempvariables during swapping.
Interactive Features
The visualizer provides an intuitive interface:Adding Elements
- Input custom numbers to add to the array
- Click “Insert” to add elements individually
- Click “Sort” to start the visualization
- Watch the algorithm find and swap minimum elements in real-time
When to Use Selection Sort
Selection Sort is best used for:
- Small datasets where simplicity is valued over efficiency
- Educational purposes to understand sorting fundamentals
- Systems with limited memory (due to O(1) space complexity)
- Situations where write operations are expensive (Selection Sort minimizes swaps)
For larger datasets or performance-critical applications, consider more efficient algorithms like Quick Sort, Merge Sort, or Heap Sort.
Try It Yourself
Visit the Selection Sort Visualizer to:- Add your own numbers to the array
- Watch the algorithm find minimum elements
- See elements swap positions in real-time
- Understand how the sorted and unsorted portions grow and shrink