S
SortViz

Sorting Visualizer

Visualize and understand sorting algorithms through interactive animations and detailed explanations

0 comparisons
0 swaps
0 ms

Algorithms

Bubble Sort

Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

Time Complexity

  • Best: O(n)
  • Average: O(n²)
  • Worst: O(n²)

Space Complexity

O(1)

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        swapped = False
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        if not swapped:
            break
    return arr

Insertion Sort

Builds the sorted array one item at a time by comparing each new element with the already-sorted elements.

Time Complexity

  • Best: O(n)
  • Average: O(n²)
  • Worst: O(n²)

Space Complexity

O(1)

def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j+1] = arr[j]
            j -= 1
        arr[j+1] = key
    return arr

Selection Sort

Divides the input list into two parts: a sorted sublist and an unsorted sublist, repeatedly selecting the smallest element.

Time Complexity

  • Best: O(n²)
  • Average: O(n²)
  • Worst: O(n²)

Space Complexity

O(1)

def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

Merge Sort

Divides the array into halves, sorts them recursively, then merges the sorted halves.

Time Complexity

  • Best: O(n log n)
  • Average: O(n log n)
  • Worst: O(n log n)

Space Complexity

O(n)

def merge_sort(arr):
    if len(arr) <= 1:
        return arr
        
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    
    return merge(left, right)
    
def merge(left, right):
    result = []
    i = j = 0
    
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
            
    result.extend(left[i:])
    result.extend(right[j:])
    return result

Quick Sort

Selects a pivot element and partitions the array around the pivot, recursively sorting the sub-arrays.

Time Complexity

  • Best: O(n log n)
  • Average: O(n log n)
  • Worst: O(n²)

Space Complexity

O(log n)

def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    
    return quick_sort(left) + middle + quick_sort(right)

Heap Sort

Builds a max heap and repeatedly extracts the maximum element, placing it at the end of the array.

Time Complexity

  • Best: O(n log n)
  • Average: O(n log n)
  • Worst: O(n log n)

Space Complexity

O(1)

def heapify(arr, n, i):
    largest = i
    left = 2 * i + 1
    right = 2 * i + 2
    
    if left < n and arr[left] > arr[largest]:
        largest = left
        
    if right < n and arr[right] > arr[largest]:
        largest = right
        
    if largest != i:
        arr[i], arr[largest] = arr[largest], arr[i]
        heapify(arr, n, largest)
        
def heap_sort(arr):
    n = len(arr)
    
    # Build max heap
    for i in range(n // 2 - 1, -1, -1):
        heapify(arr, n, i)
        
    # Extract elements one by one
    for i in range(n - 1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]
        heapify(arr, i, 0)
        
    return arr

Counting Sort

Counts the number of objects having distinct key values, then calculates positions of each object in the output sequence.

Time Complexity

  • Best: O(n+k)
  • Average: O(n+k)
  • Worst: O(n+k)

Space Complexity

O(k)

def counting_sort(arr):
    max_val = max(arr)
    m = max_val + 1
    count = [0] * m
    
    for a in arr:
        count[a] += 1
        
    i = 0
    for a in range(m):
        for c in range(count[a]):
            arr[i] = a
            i += 1
            
    return arr

Radix Sort

Processes digits from least significant to most significant, using a stable sort for each digit position.

Time Complexity

  • Best: O(nk)
  • Average: O(nk)
  • Worst: O(nk)

Space Complexity

O(n+k)

def counting_sort_for_radix(arr, exp):
    n = len(arr)
    output = [0] * n
    count = [0] * 10
    
    for i in range(n):
        index = arr[i] // exp
        count[index % 10] += 1
        
    for i in range(1, 10):
        count[i] += count[i - 1]
        
    i = n - 1
    while i >= 0:
        index = arr[i] // exp
        output[count[index % 10] - 1] = arr[i]
        count[index % 10] -= 1
        i -= 1
        
    for i in range(n):
        arr[i] = output[i]
        
def radix_sort(arr):
    max_val = max(arr)
    exp = 1
    
    while max_val // exp > 0:
        counting_sort_for_radix(arr, exp)
        exp *= 10
        
    return arr

Practice Problems