Sorting algorithm visualization


  1. Booble sort

Booble sort

Booble sort is perhaps the simplest sorting algorithm ever. It repeatly iterates through an array of elements, compares each pair of adjencet elements and swaps them if they are not in the right order. Algorithm ends if going though the whole array doesn't swap any elements.

import alvi
client = alvi.connect()

array = client.create_array()
array.generate_data("random", 20)

changed = True
right_marker = array.create_marker("right", array.size()-1)
array.sync()
right = array.size()
array.stats.comparisons = 0
while changed:
    changed = False
    for j in range(1, right):
        item_a = array[j]
        item_b = array[j - 1]
        if item_a < item_b:
            array[j], array[j-1] = array[j-1], array[j]
            changed = True
        array.stats.comparisons += 1
    right -= 1
    right_marker.move(right)
    array.sync()