Sorting Algorithm Visualizer
← Back to Home
Controls
Choose Algorithm:
Bubble Sort
Selection Sort
Insertion Sort
Heap Sort
Counting Sort
Array Length:
Or Enter Array (comma separated):
Auto-generate array
Generate Array
Start Sorting
Current Array:
Bubble Sort Algorithm
Steps:
Start from the first element of the array.
Compare the current element with the next one.
If the current element is greater, swap them.
Continue comparing adjacent elements until the end of the array.
After each pass, the largest element "bubbles up" to its correct position at the end.
Repeat the process for the remaining unsorted part of the array.
If no swaps occur in a pass, the array is sorted and the algorithm stops.
Best Case:
O(n)
Average Case:
O(n²)
Worst Case:
O(n²)
Space Complexity:
O(1)
Selection Sort Algorithm
Steps:
Start from the first element of the array.
Find the minimum element from the unsorted part of the array.
Swap the minimum element with the first element of the unsorted part.
Move the boundary of the sorted and unsorted part one position forward.
Repeat the process until the entire array becomes sorted.
Best Case:
O(n²)
Average Case:
O(n²)
Worst Case:
O(n²)
Space Complexity:
O(1)
Insertion Sort Algorithm
Steps:
Start from the second element of the array (index 1).
Compare the current element with elements before it.
Shift all elements that are greater than the current element one position to the right.
Insert the current element at its correct position in the sorted part of the array.
Move to the next element and repeat steps 2–4 until the array is completely sorted.
Best Case:
O(n)
Average Case:
O(n²)
Worst Case:
O(n²)
Space Complexity:
O(1)
Heap Sort Algorithm
Steps:
Build a max heap from the input array.
Swap the root (maximum value) of the heap with the last element of the heap.
Reduce the heap size by one and heapify the root element to maintain the max heap property.
Repeat steps 2–3 until the heap size becomes 1.
At the end, the array is sorted in ascending order.
Time Complexity:
O(n log n)
Space Complexity:
O(1)
Counting Sort Algorithm
Steps:
Find the maximum element in the array.
Create a count array of size (maximum + 1) and initialize all values to 0.
Count the occurrence of each element in the input array and store it in the count array.
Modify the count array such that each element at index i stores the sum of previous counts (cumulative count).
Build the output array by placing elements at their correct positions using the count array, and decrease the count after placing each element.
Copy the sorted elements from the output array back to the original array.
Time Complexity:
O(n + k)
Space Complexity:
O(n + k)
Visualization
Heap Tree Visualization