KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > SortAlgo


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package examples;
18
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.NDC;
21
22 /**
23    Example code for log4j to viewed in conjunction with the {@link
24    examples.Sort Sort} class.
25       
26    <p>SortAlgo uses the bubble sort algorithm to sort an integer
27    array. See also its <b><a HREF="doc-files/SortAlgo.java">source
28    code</a></b>.
29
30    @author Ceki G&uuml;lc&uuml; */

31 public class SortAlgo {
32
33   final static String JavaDoc className = SortAlgo.class.getName();
34   final static Logger LOG = Logger.getLogger(className);
35   final static Logger OUTER = Logger.getLogger(className + ".OUTER");
36   final static Logger INNER = Logger.getLogger(className + ".INNER");
37   final static Logger DUMP = Logger.getLogger(className + ".DUMP");
38   final static Logger SWAP = Logger.getLogger(className + ".SWAP");
39
40   int[] intArray;
41
42   SortAlgo(int[] intArray) {
43     this.intArray = intArray;
44   }
45     
46   void bubbleSort() {
47     LOG.info( "Entered the sort method.");
48
49     for(int i = intArray.length -1; i >= 0 ; i--) {
50       NDC.push("i=" + i);
51       OUTER.debug("in outer loop.");
52       for(int j = 0; j < i; j++) {
53     NDC.push("j=" + j);
54     // It is poor practice to ship code with log staments in tight loops.
55
// We do it anyway in this example.
56
INNER.debug( "in inner loop.");
57          if(intArray[j] > intArray[j+1])
58        swap(j, j+1);
59     NDC.pop();
60       }
61       NDC.pop();
62     }
63   }
64
65   void dump() {
66     if(! (this.intArray instanceof int[])) {
67       DUMP.error("Tried to dump an uninitialized array.");
68       return;
69     }
70     DUMP.info("Dump of integer array:");
71     for(int i = 0; i < this.intArray.length; i++) {
72       DUMP.info("Element [" + i + "]=" + this.intArray[i]);
73     }
74   }
75
76   void swap(int l, int r) {
77     // It is poor practice to ship code with log staments in tight
78
// loops or code called potentially millions of times.
79
SWAP.debug( "Swapping intArray["+l+"]=" + intArray[l] +
80                          " and intArray["+r+"]=" + intArray[r]);
81     int temp = this.intArray[l];
82     this.intArray[l] = this.intArray[r];
83     this.intArray[r] = temp;
84   }
85 }
86
87
Popular Tags