2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.misc-tests / sort2.c
blob06b150b93d614d5930c46a828c06544703173c17
1 /* Simple test program: bubble sort of a fixed table. */
2 /* This demonstrates some of the compiler's common-subexpression*/
3 /* elimination capabilities. For example, inspect the code */
4 /* generated for procedure Sort_array. See the Programmer's */
5 /* Guide for how to request an assembly listing on your host. */
7 typedef unsigned char boolean;
9 void Sort_array();
10 int Tab[100];
12 main () {
13 int I,J,K,L;
15 for (L = 0; L < 1000; L++) {
16 /* Initialize the table that will be sorted. */
17 K = 0;
18 for (I = 9; I >= 0; I--)
19 for (J = I*10; J < (I+1)*10; J++)
20 Tab[K++] = J&1 ? J+1 : J-1;
22 /* Print_array(); */
23 Sort_array(Tab,99); /* Sort it. */
24 /* Print_array(); */
26 return 0;
29 void Sort_array(Tab,Last) int Tab[]; int Last; {
30 boolean Swap;
31 int Temp,I;
32 do {
33 Swap = 0;
34 for (I = 0; I<Last; I++)
35 if (Tab[I] > Tab[I+1]) {
36 Temp = Tab[I];
37 Tab[I] = Tab[I+1];
38 Tab[I+1] = Temp;
39 Swap = 1;
42 while (Swap);
46 void Print_array() {
47 int I,J;
48 /*printf("\nArray Contents:\n");*/
49 for (I=0; I<=9; I++) {
50 /*printf("%5d:",10*I); */
51 for (J=0; J<=9; J++); /*printf("%5d",Tab[10*I+J]); */
52 /* printf("\n");*/