CoolFace
Apppublic

Rennie44/Sorting-Algorithm-Visualization

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
bubble_sort.py31 linesDownload Raw Back to algorithms
1"""2Bubble sort that records each step in a simple format,3suitable for HTML-based visualization.4"""5 6def bubble_sort_steps(arr):7    a = list(arr)8    steps = []9 10    steps.append({"array": a.copy(), "compare": None, "swapped": None})11 12    n = len(a)13    for i in range(n):14        swapped_any = False15 16        for j in range(0, n - i - 1):17            steps.append({"array": a.copy(), "compare": (j, j+1), "swapped": False})18 19            if a[j] > a[j + 1]:20                a[j], a[j + 1] = a[j + 1], a[j]21                swapped_any = True22                steps.append({"array": a.copy(), "compare": (j, j+1), "swapped": True})23 24        steps.append({"array": a.copy(), "compare": None, "swapped": swapped_any})25 26        if not swapped_any:27            break28 29    steps.append({"array": a.copy(), "compare": None, "swapped": False})30    return steps31