summaryrefslogtreecommitdiff
path: root/ecomp-c/tests/example_insertion_sort.e
diff options
context:
space:
mode:
Diffstat (limited to 'ecomp-c/tests/example_insertion_sort.e')
-rw-r--r--ecomp-c/tests/example_insertion_sort.e40
1 files changed, 40 insertions, 0 deletions
diff --git a/ecomp-c/tests/example_insertion_sort.e b/ecomp-c/tests/example_insertion_sort.e
new file mode 100644
index 0000000..21e47a8
--- /dev/null
+++ b/ecomp-c/tests/example_insertion_sort.e
@@ -0,0 +1,40 @@
+/*
+ * sort an array (insertion sort)
+ *
+ */
+
+module example_insertion_sort;
+
+const
+ N : integer = 10;
+
+var
+ i, j, x : integer;
+ a : array N of integer;
+
+begin
+ // initialize the array (no integer array initializers - yet)
+ a[0] := 7;
+ a[1] := 4;
+ a[2] := 0;
+ a[3] := 8;
+ a[4] := 5;
+ a[5] := 3;
+ a[6] := 1;
+ a[7] := 9;
+ a[8] := 6;
+ a[9] := 2;
+
+ // sort it
+ i := 1;
+ while i < N do
+ x := a[i];
+ j := i;
+ while ( j > 0 ) and ( x < a[j-1] ) do
+ a[j] := a[j-1];
+ j := j-1;
+ end;
+ a[j] := x;
+ i := i+1;
+ end
+end