summaryrefslogtreecommitdiff
path: root/ecomp-c/tests/example_insertion_sort.e
blob: 21e47a85f26b8434cbea892222e37aeaa825dac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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