Files
pplus-web/mojo/stress/bubble-sort.mojo
T
2026-10-05 03:03:38 +00:00

22 lines
657 B
Mojo

# probe: sorting a List[Int] in place with nested loops and a swap
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [1, 2, 5, 5, 6, 9]
# expect-stdout: 0
def bubble_sort(mut xs: List[Int]):
var n = len(xs)
for i in range(n):
for j in range(n - 1 - i):
if xs[j] > xs[j + 1]:
var tmp = xs[j]
xs[j] = xs[j + 1]
xs[j + 1] = tmp
def main():
var xs: List[Int] = [5, 2, 9, 1, 5, 6]
bubble_sort(xs)
print(xs)
var empty = List[Int]()
bubble_sort(empty)
print(len(empty))