1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00
ZoKrates/zokrates_cli/examples/book/bubblesort.zok
2022-06-28 19:23:45 +02:00

21 lines
497 B
Text

def swap(u32 a, u32 mut b, bool c) -> (u32, u32) {
u32 a_prime = c ? b : a;
b = c ? a : b;
return (a_prime, b);
}
def bubblesort<N>(u32[N] mut a) -> u32[N] {
for u32 i in 0..(N-1) {
for u32 j in 0..(N-1-i) {
bool need_swap = a[j + 1] < a[j];
(u32, u32) swapped = swap(a[j], a[j + 1], need_swap);
a[j] = swapped.0;
a[j + 1] = swapped.1;
}
}
return a;
}
def main(u32[10] a) -> u32[10] {
return bubblesort(a);
}