From 3a5c0abbefdd8e048cc2ba45d74a1f9cb0da89d4 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Mon, 18 Jul 2022 11:13:27 -0700 Subject: [PATCH] Introduce bubblesort_tuple example --- examples/sorting/bubblesort_tuple/.gitignore | 2 + examples/sorting/bubblesort_tuple/README.md | 8 + .../inputs/bubblesort_tuple.in | 12 + .../sorting/bubblesort_tuple/program.json | 10 + .../sorting/bubblesort_tuple/src/main.leo | 285 ++++++++++++++++++ 5 files changed, 317 insertions(+) create mode 100644 examples/sorting/bubblesort_tuple/.gitignore create mode 100644 examples/sorting/bubblesort_tuple/README.md create mode 100644 examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in create mode 100644 examples/sorting/bubblesort_tuple/program.json create mode 100644 examples/sorting/bubblesort_tuple/src/main.leo diff --git a/examples/sorting/bubblesort_tuple/.gitignore b/examples/sorting/bubblesort_tuple/.gitignore new file mode 100644 index 0000000000..b28696155d --- /dev/null +++ b/examples/sorting/bubblesort_tuple/.gitignore @@ -0,0 +1,2 @@ +outputs/ +build/ diff --git a/examples/sorting/bubblesort_tuple/README.md b/examples/sorting/bubblesort_tuple/README.md new file mode 100644 index 0000000000..87f6687360 --- /dev/null +++ b/examples/sorting/bubblesort_tuple/README.md @@ -0,0 +1,8 @@ +# bubblesort_tuple.aleo + +## Build Guide + +To compile this Aleo program, run: +```bash +aleo build +``` diff --git a/examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in b/examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in new file mode 100644 index 0000000000..a435f71f0e --- /dev/null +++ b/examples/sorting/bubblesort_tuple/inputs/bubblesort_tuple.in @@ -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; + + diff --git a/examples/sorting/bubblesort_tuple/program.json b/examples/sorting/bubblesort_tuple/program.json new file mode 100644 index 0000000000..5bf01f4eb2 --- /dev/null +++ b/examples/sorting/bubblesort_tuple/program.json @@ -0,0 +1,10 @@ +{ + "program": "bubblesort_tuple.aleo", + "version": "0.0.0", + "description": "", + "development": { + "private_key": "APrivateKey1zkpBqRv2cwkSiR4hQ3Tb4AZFD3XzdwPqV9QsEykTKBV1YKT", + "address": "aleo1ht2a9q0gsd38j0se4t9lsfulxgqrens2vgzgry3pkvs93xrrzu8s892zn7" + }, + "license": "MIT" +} \ No newline at end of file diff --git a/examples/sorting/bubblesort_tuple/src/main.leo b/examples/sorting/bubblesort_tuple/src/main.leo new file mode 100644 index 0000000000..c04a4ffc38 --- /dev/null +++ b/examples/sorting/bubblesort_tuple/src/main.leo @@ -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); +} \ No newline at end of file