回溯法,代码如下:
/**
* author Akalius Kung 2008-2-8
**/
public class Queen {
private int[] grids; // location in each row, index is each row, array value is location of each queen
private int n;
private static int sum;
public Queen() {
init(8);
}
public Queen(int n) {
this.n = n;
grids=new int[n];
for(int i=0;i<n;i++){
grids[i]=0;
}
}
private void init(int n){
...
/**
* author Akalius Kung 2008-2-9
**/
public class HeapSort {
private int heapLen;
private int[] sort(int[] array){
heapLen=array.length;
buildHeap(array); // init the heap
for(int i=heapLen-1;i>0;i--){ // swap root and last node, up to down
swap(array,i,0);
heapLen--;
heapify(array,0); // reheapify the root node from 0 to n-1
}
return array;
}
private void buildHeap(...
/**
* author Akalius Kung 2008-2-9
**/
public class HeapSort {
private int heapLen;
private int[] sort(int[] array){
heapLen=array.length;
buildHeap(array); // init the heap
for(int i=heapLen-1;i>0;i--){ // swap root and last node, up to down
swap(array,i,0);
heapLen--;
heapify(array,0); // reheapify the root node from 0 to n-1
}
return array;
}
private void buildHeap(i...
/**
* author Akalius Kung 2008-2-9
**/
public class InsertionSort {
private int[] sort(int[] array){
for(int j=1;j<array.length;j++){
int swapLoc = j; // init the location where to insert
for(int i=j-1;i>=0;i--){ // get the location where to insert
if(array[i]>array[j]){
swapLoc=i;
}
}
// backward the elems between swapLoc and j
int temp=array[j];
for(int k=j-1;k...