Browse Source

Bubble sort algorithm in AerScript.

release/v0.1
Rafal Kupiec 1 year ago
parent
commit
13ea9825b8
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
2 changed files with 45 additions and 0 deletions
  1. +27
    -0
      tests/bubble_sort.aer
  2. +18
    -0
      tests/bubble_sort.exp

+ 27
- 0
tests/bubble_sort.aer View File

@ -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));
}
}

+ 18
- 0
tests/bubble_sort.exp View File

@ -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)
}

Loading…
Cancel
Save