1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00
ZoKrates/zokrates_cli/examples/book/bubblesort.zok
2022-06-21 15:03:53 +02:00

22 lines
487 B
Text

def swap(u32 a, u32 b, bool c) -> (u32, u32) {
u32 a_prime = c ? b : a;
b = c ? a : b;
return (a_prime, b);
}
def bubblesort<N>(u32[N] 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);
}