mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-28 09:02:58 +03:00
Introduce bubblesort_tuple example
This commit is contained in:
parent
41b985f923
commit
3a5c0abbef
2
examples/sorting/bubblesort_tuple/.gitignore
vendored
Normal file
2
examples/sorting/bubblesort_tuple/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
outputs/
|
||||
build/
|
8
examples/sorting/bubblesort_tuple/README.md
Normal file
8
examples/sorting/bubblesort_tuple/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# bubblesort_tuple.aleo
|
||||
|
||||
## Build Guide
|
||||
|
||||
To compile this Aleo program, run:
|
||||
```bash
|
||||
aleo build
|
||||
```
|
12
examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in
Normal file
12
examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in
Normal file
@ -0,0 +1,12 @@
|
||||
// The program input for bubblesort_tuple/src/main.leo
|
||||
[bubble_sort]
|
||||
arr0: u32 = 13u32;
|
||||
arr1: u32 = 2u32;
|
||||
arr2: u32 = 4u32;
|
||||
arr3: u32 = 3u32;
|
||||
arr4: u32 = 5u32;
|
||||
arr5: u32 = 10u32;
|
||||
arr6: u32 = 7u32;
|
||||
arr7: u32 = 1u32;
|
||||
|
||||
|
10
examples/sorting/bubblesort_tuple/program.json
Normal file
10
examples/sorting/bubblesort_tuple/program.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"program": "bubblesort_tuple.aleo",
|
||||
"version": "0.0.0",
|
||||
"description": "",
|
||||
"development": {
|
||||
"private_key": "APrivateKey1zkpBqRv2cwkSiR4hQ3Tb4AZFD3XzdwPqV9QsEykTKBV1YKT",
|
||||
"address": "aleo1ht2a9q0gsd38j0se4t9lsfulxgqrens2vgzgry3pkvs93xrrzu8s892zn7"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
285
examples/sorting/bubblesort_tuple/src/main.leo
Normal file
285
examples/sorting/bubblesort_tuple/src/main.leo
Normal file
@ -0,0 +1,285 @@
|
||||
// Executes the bubble sorting algorithm.
|
||||
// The original algorithm is given below.
|
||||
//
|
||||
// for i in 0..7 {
|
||||
// for j in 0..7-i {
|
||||
// // Move the smaller elements forward
|
||||
// if arr[j+1] < arr[j] {
|
||||
// // Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
// let swap = arr[j];
|
||||
// arr[j] = arr[j+1];
|
||||
// arr[j+1] = swap;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Note that the implementation below uses tuples instead of arrays.
|
||||
// The implementation also manually unrolls the loop.
|
||||
|
||||
function bubble_sort(
|
||||
arr0: u32,
|
||||
arr1: u32,
|
||||
arr2: u32,
|
||||
arr3: u32,
|
||||
arr4: u32,
|
||||
arr5: u32,
|
||||
arr6: u32,
|
||||
arr7: u32,
|
||||
) -> (u32, u32, u32, u32, u32, u32, u32, u32) {
|
||||
|
||||
// Unroll the loops.
|
||||
|
||||
// (i, j) = (0, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 2).
|
||||
// Move the smaller elements forward.
|
||||
if arr3 < arr2 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr2;
|
||||
arr2 = arr3;
|
||||
arr3 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 3).
|
||||
// Move the smaller elements forward.
|
||||
if arr4 < arr3 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr3;
|
||||
arr3 = arr4;
|
||||
arr4 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 4).
|
||||
// Move the smaller elements forward.
|
||||
if arr5 < arr4 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr4;
|
||||
arr4 = arr5;
|
||||
arr5 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 5).
|
||||
// Move the smaller elements forward.
|
||||
if arr6 < arr5 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr5;
|
||||
arr5 = arr6;
|
||||
arr6 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (0, 6).
|
||||
// Move the smaller elements forward.
|
||||
if arr7 < arr6 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr6;
|
||||
arr6 = arr7;
|
||||
arr7 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 2).
|
||||
// Move the smaller elements forward.
|
||||
if arr3 < arr2 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr2;
|
||||
arr2 = arr3;
|
||||
arr3 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 3).
|
||||
// Move the smaller elements forward.
|
||||
if arr4 < arr3 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr3;
|
||||
arr3 = arr4;
|
||||
arr4 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 4).
|
||||
// Move the smaller elements forward.
|
||||
if arr5 < arr4 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr4;
|
||||
arr4 = arr5;
|
||||
arr5 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (1, 5).
|
||||
// Move the smaller elements forward.
|
||||
if arr6 < arr5 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr5;
|
||||
arr5 = arr6;
|
||||
arr6 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (2, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (2, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (2, 2).
|
||||
// Move the smaller elements forward.
|
||||
if arr3 < arr2 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr2;
|
||||
arr2 = arr3;
|
||||
arr3 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (2, 3).
|
||||
// Move the smaller elements forward.
|
||||
if arr4 < arr3 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr3;
|
||||
arr3 = arr4;
|
||||
arr4 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (2, 4).
|
||||
// Move the smaller elements forward.
|
||||
if arr5 < arr4 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr4;
|
||||
arr4 = arr5;
|
||||
arr5 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (3, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (3, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (3, 2).
|
||||
// Move the smaller elements forward.
|
||||
if arr3 < arr2 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr2;
|
||||
arr2 = arr3;
|
||||
arr3 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (3, 3).
|
||||
// Move the smaller elements forward.
|
||||
if arr4 < arr3 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr3;
|
||||
arr3 = arr4;
|
||||
arr4 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (4, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (4, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (4, 2).
|
||||
// Move the smaller elements forward.
|
||||
if arr3 < arr2 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr2;
|
||||
arr2 = arr3;
|
||||
arr3 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (5, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (5, 1).
|
||||
// Move the smaller elements forward.
|
||||
if arr2 < arr1 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr1;
|
||||
arr1 = arr2;
|
||||
arr2 = temp;
|
||||
}
|
||||
|
||||
// (i, j) = (6, 0).
|
||||
// Move the smaller elements forward.
|
||||
if arr1 < arr0 {
|
||||
// Swap the elements at indexes ‘j‘ and ‘j+1‘
|
||||
let temp: u32 = arr0;
|
||||
arr0 = arr1;
|
||||
arr1 = temp;
|
||||
}
|
||||
|
||||
return (arr0, arr1, arr2, arr3, arr4, arr5, arr6, arr7);
|
||||
}
|
Loading…
Reference in New Issue
Block a user