1 16 17 package examples; 18 19 import org.apache.log4j.Logger; 20 import org.apache.log4j.NDC; 21 22 31 public class SortAlgo { 32 33 final static String 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 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 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 |