Array Configuration
Algorithm Information
Two-Pointer Approach
Uses a slow pointer to track the next position for non-zero elements. After processing, fills the remaining positions with zeros.
Snowball Technique
Treats zeros like a snowball that grows as they're encountered. Non-zero elements are swapped with the first zero in the snowball.
Insertion-Sort Like
Iterates from right to left, shifting non-zeros left to make space for zeros at the end. Similar to insertion sort logic.
Selection Sort Variant
Adapts selection sort by scanning right-to-left to find zeros and swapping them with the last non-zero element.
Bubble Sort Variant
Adapts bubble sort by bubbling zeros to the right through adjacent swaps.
Counting Non-Zeros
Counts non-zero elements, writes them to a new array, then fills remaining positions with zeros. Simple but requires extra space.
Algorithm | Time | Space | Stable | Best For |
---|---|---|---|---|
Two-Pointer | O(n) | O(1) | Yes | Most cases |
Snowball | O(n) | O(1) | Yes | In-place swapping |
Insertion-Sort Like | O(n²) | O(1) | Yes | Educational purposes |
Selection Variant | O(n²) | O(1) | No | Educational |
Bubble Variant | O(n²) | O(1) | Yes | Educational |
Counting | O(n) | O(n) | Yes | Simplicity |
Algorithm Visualization
Algorithm Explanation
Select an algorithm and click "Run Algorithm" to start the visualization. The Two-Pointer approach is recommended for most cases as it provides optimal O(n) time complexity with O(1) space.