递归

来源:互联网 发布:php幸运大转盘源码 编辑:程序博客网 时间:2024/04/28 18:52

分治算法,两个对自身的递归只有一个真正执行

 汉诺塔:

        class TowersApp{

               static int nDisks = 3;

               public static void main(String[]args){

                      doTowers(nDisks,’A’,’B’,’C’);

}

public static void doTowers(int topN,char from ,char inter,char to){

       if(topN==1){

              System.out.println(“disk1 from”+from+”to”+to);

}else{

       doTowers(topN-1,from,to,inter);

       System.out.println(“disk“+topN+” from”+from+”to”+to);

       doTower(topN-1,inter,from,to);

}

}

}

归并排序,两个有序数组  O(N*logN)

        public static void merge(int[]arrayA,int sizeA,int[] arrayB,int sizeB,int[] arrayC){

        int aDex =0,bDex=0,cDex=0;

        while(aDex<sizeA &&bDex<sizeB)

               if(arrayA[aDex]<arrayB[bDex])

              arrayC[cDex++] =arrayA[aDex++];

else

       arrayC[cDex++]=arrayB[bDex++];

              while(aDex<sizeA)

                     arrayC[cDex++]= arrayA[aDex++];

while(aDex<sizeB)

                     arrayC[cDex++]= arrayB[bDex++];

 

 

}

 

递归:

        Eg:一个数的乘方,背包问题

 

a, 有序数组二分查找:

 

    package datastrut;

/**

 * 有序 ,二分查找法

 *

 * */

public class OrderArray {

 

  //class OrdArray{

     private long[] a;

     private int nElems;

    

     public OrderArray(int max){

         a =  new long[max];

         nElems = 0;

     }

     public int size(){

         return nElems;

     }

     public int find(long searchKey){

         int lowerBound = 0;

         int upperBound = nElems -1;

         int curIn;

        

         while(true){

            curIn =(lowerBound+upperBound)/2;

            if(a[curIn] ==searchKey){

                return curIn;

            }else if(lowerBound >upperBound){

                return nElems;

            }else{

                if(a[curIn] <searchKey){

                   lowerBound= curIn + 1;

                }else{

                   upperBound= curIn - 1;

                }

            }

         }

     }

  //递归二分查找

     private int recFind(longsearchKey,int lowerBound,int upperBount){

         int curIn;

         curIn = (lowerBound +upperBound)/2;

         if(a[curIn]==searchKey){

    return curIn;

}

else if (lowerBound > upperBound){

return nElemnts;

}else{

    if(a[curIn] < searchKey){

       returnrecFind(searchKey,curIn+1,upperBound);

}else{

    returnrecFind(searchKey,lowerBound+1,curIn-1);

}

}

 

}

    

     public void insert(long value){

         int j ;

         for(j = 0;j < nElems; j++){

            if(a[j] > value)

                break;

         }

         for(int k = nElems; k > j ; k--){

            a[k] = a[k-1];

         }

         a[j] = value;

         nElems++;

     }

     public boolean delete(long value){

         int i = find(value);

         if(i == nElems)

            return false;

         else{

            for(int k = i; k<nElems; k++){

                a[k] = a[k+1];

            }

            nElems--;

            return true;

         }

     }

     public void display(){

         for(int i = 0; i < nElems; i++)

            System.out.print(a[i] + " ");

         System.out.println(" ");

     }

    

  //}

  public static void main(String[]args) {

    

     int maxSize = 100;

     OrderArray arr= new OrderArray(maxSize);

     arr.insert(77);

     arr.insert(23);

     arr.insert(43);

     arr.insert(55);

     arr.insert(65);

     arr.insert(75);

     arr.insert(32);

     arr.insert(54);

    

     int searchKey = 33;

     if(arr.find(searchKey)!= arr.size())

         System.out.println("found "+searchKey);

     else

         System.out.println("not found "+ searchKey);

 

     arr.display();

     arr.delete(21);

     arr.delete(55);

     arr.display();

  }

}


原创粉丝点击