FileDocCategorySizeDatePackage
SortAlgo.javaAPI DocApache log4j 1.2.152872Sat Aug 25 00:09:44 BST 2007examples

SortAlgo

public class SortAlgo extends Object
Example code for log4j to viewed in conjunction with the {@link examples.Sort Sort} class.

SortAlgo uses the bubble sort algorithm to sort an integer array. See also its source code.

author
Ceki Gülcü

Fields Summary
static final String
className
static final Logger
LOG
static final Logger
OUTER
static final Logger
INNER
static final Logger
DUMP
static final Logger
SWAP
int[]
intArray
Constructors Summary
SortAlgo(int[] intArray)


    
    this.intArray = intArray;
  
Methods Summary
voidbubbleSort()

    LOG.info( "Entered the sort method.");

    for(int i = intArray.length -1; i >= 0  ; i--) {
      NDC.push("i=" + i);
      OUTER.debug("in outer loop.");
      for(int j = 0; j < i; j++) {
	NDC.push("j=" + j);
	// It is poor practice to ship code with log staments in tight loops.
	// We do it anyway in this example.
	INNER.debug( "in inner loop.");
         if(intArray[j] > intArray[j+1])
	   swap(j, j+1);
	NDC.pop();
      }
      NDC.pop();
    }
  
voiddump()

    
    if(! (this.intArray instanceof int[])) {
      DUMP.error("Tried to dump an uninitialized array.");
      return;
    }
    DUMP.info("Dump of integer array:");
    for(int i = 0; i < this.intArray.length; i++) {
      DUMP.info("Element [" + i + "]=" + this.intArray[i]);
    }    
  
voidswap(int l, int r)

    // It is poor practice to ship code with log staments in tight
    // loops or code called potentially millions of times.
    SWAP.debug( "Swapping intArray["+l+"]=" + intArray[l] +
	                     " and intArray["+r+"]=" + intArray[r]);
    int temp = this.intArray[l];
    this.intArray[l] = this.intArray[r];
    this.intArray[r] = temp;