From 13ea9825b8c699ec19b999ba1fd778d29609a0b4 Mon Sep 17 00:00:00 2001 From: belliash Date: Sat, 20 Apr 2019 16:56:53 +0200 Subject: [PATCH] Bubble sort algorithm in AerScript. --- tests/bubble_sort.aer | 27 +++++++++++++++++++++++++++ tests/bubble_sort.exp | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/bubble_sort.aer create mode 100644 tests/bubble_sort.exp diff --git a/tests/bubble_sort.aer b/tests/bubble_sort.aer new file mode 100644 index 0000000..475f3b9 --- /dev/null +++ b/tests/bubble_sort.aer @@ -0,0 +1,27 @@ +class Program { + + public static int[] bubbleSort(int[] $arr) { + int $n = sizeof($arr); + for(int $i = 1; $i < $n; $i++) { + bool $flag = false; + for(int $j = $n - 1; $j >= $i; $j--) { + if($arr[$j-1] > $arr[$j]) { + int $tmp = $arr[$j - 1]; + $arr[$j - 1] = $arr[$j]; + $arr[$j] = $tmp; + $flag = true; + } + } + if(!$flag) { + break; + } + } + return $arr; + } + + public void main() { + int[] $arr = {7, 9, 1, 2, 5, 6, 10, 14}; + var_dump(Program::bubbleSort($arr)); + } + +} \ No newline at end of file diff --git a/tests/bubble_sort.exp b/tests/bubble_sort.exp new file mode 100644 index 0000000..2af19ce --- /dev/null +++ b/tests/bubble_sort.exp @@ -0,0 +1,18 @@ +array(int, 8) { + [0] => + int(1) + [1] => + int(2) + [2] => + int(5) + [3] => + int(6) + [4] => + int(7) + [5] => + int(9) + [6] => + int(10) + [7] => + int(14) + } \ No newline at end of file