mageec  0.1.0
MAchine Guided Energy Efficient Compilation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sort.c
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Copyright 2010 Rulequest Research Pty Ltd. */
4 /* */
5 /* This file is part of C5.0 GPL Edition, a single-threaded version */
6 /* of C5.0 release 2.07. */
7 /* */
8 /* C5.0 GPL Edition is free software: you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License as */
10 /* published by the Free Software Foundation, either version 3 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* C5.0 GPL Edition is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU General Public License */
19 /* (gpl.txt) along with C5.0 GPL Edition. If not, see */
20 /* */
21 /* <http://www.gnu.org/licenses/>. */
22 /* */
23 /*************************************************************************/
24 
25 
26 
27 /*************************************************************************/
28 /* */
29 /* Sorting utilities */
30 /* ----------------- */
31 /* */
32 /*************************************************************************/
33 
34 
35 #include "defns.i"
36 #include "extern.i"
37 
38 #define SwapSRec(a,b) {Xab=SRec[a]; SRec[a]=SRec[b]; SRec[b]=Xab;}
39 
40 
41 /*************************************************************************/
42 /* */
43 /* Sort elements Fp to Lp of SRec */
44 /* */
45 /*************************************************************************/
46 
47 
48 void Cachesort(CaseNo Fp, CaseNo Lp, SortRec *SRec)
49 /* --------- */
50 {
51  CaseNo i, Middle, High;
52  ContValue Thresh, Val;
53  SortRec Xab;
54 
55 
56  while ( Fp < Lp )
57  {
58  Thresh = SRec[(Fp+Lp) / 2].V;
59 
60  /* Divide elements into three groups:
61  Fp .. Middle-1: values < Thresh
62  Middle .. High: values = Thresh
63  High+1 .. Lp: values > Thresh */
64 
65  for ( Middle = Fp ; SRec[Middle].V < Thresh ; Middle++ )
66  ;
67 
68  for ( High = Lp ; SRec[High].V > Thresh ; High-- )
69  ;
70 
71  for ( i = Middle ; i <= High ; )
72  {
73  if ( (Val = SRec[i].V) < Thresh )
74  {
75  SwapSRec(Middle, i);
76  Middle++;
77  i++;
78  }
79  else
80  if ( Val > Thresh )
81  {
82  SwapSRec(High, i);
83  High--;
84  }
85  else
86  {
87  i++;
88  }
89  }
90 
91  /* Sort the first group */
92 
93  Cachesort(Fp, Middle-1, SRec);
94 
95  /* Continue with the last group */
96 
97  Fp = High+1;
98  }
99 }
100 
101 
102 
103 /*************************************************************************/
104 /* */
105 /* Sort cases from Fp to Lp on attribute Att */
106 /* */
107 /*************************************************************************/
108 
109 
110 void Quicksort(CaseNo Fp, CaseNo Lp, Attribute Att)
111 /* --------- */
112 {
113  CaseNo i, Middle, High;
114  ContValue Thresh, Val;
115 
116  if ( Fp < Lp )
117  {
118  Thresh = CVal(Case[(Fp+Lp) / 2], Att);
119 
120  /* Divide cases into three groups:
121  Fp .. Middle-1: values < Thresh
122  Middle .. High: values = Thresh
123  High+1 .. Lp: values > Thresh */
124 
125  for ( Middle = Fp ; CVal(Case[Middle], Att) < Thresh ; Middle++ )
126  ;
127 
128  for ( High = Lp ; CVal(Case[High], Att) > Thresh ; High-- )
129  ;
130 
131  for ( i = Middle ; i <= High ; )
132  {
133  if ( (Val = CVal(Case[i], Att)) < Thresh )
134  {
135  Swap(Middle, i);
136  Middle++;
137  i++;
138  }
139  else
140  if ( Val > Thresh )
141  {
142  Swap(High, i);
143  High--;
144  }
145  else
146  {
147  i++;
148  }
149  }
150 
151  /* Sort the first and third groups */
152 
153  Quicksort(Fp, Middle-1, Att);
154  Quicksort(High+1, Lp, Att);
155  }
156 }