20171015算法学习总结

来源:互联网 发布:linux能ghost吗 编辑:程序博客网 时间:2024/06/10 10:15

1.队列

package algorithm;public class Queue {    private Object[] objects;    private int size;    private int head;    private int end;    public Queue(int size) {        this.objects = new Object[size];        this.size = 0;        this.head = 0;        this.end = 0;    }     public void push(Object object) throws Exception {         if(this.size>objects.length)             throw new Exception("Queue is full");         objects[end++]=object;         size++;     }     public Object pop() throws Exception{         if(this.size==0)             throw new Exception("Queue is empty!");         if(head==objects.length)             this.head=0;         size--;         return objects[head++];     }     public Object peek() throws Exception{         if(this.size==0)             throw new Exception("Queue is empty!");         return objects[head];     }    public boolean isEmpty(){        return size==0;    }    public boolean isFull(){        return size==objects.length;    }    public int getSize() {        return size;    }}

2.栈

package algorithm;public class Stack {    private Object[] objects;    private int head;    private int size;    public Stack(int size){        objects=new Object[size];        this.head=0;        this.size=0;    }    public void push(Object object) throws Exception{        if(this.size==objects.length)            throw new Exception("this stack is full");        objects[head++]=object;        size++;    }    public Object pop() throws Exception{        if(size==0)            throw new Exception("this stack is empty");        size--;        return objects[--head];    }}

3.斐波那契数列

package algorithm;public class Fibonacci {    //递归方法    public static void main(String[] args) {        System.out.println(f(5));    }    public static int f(int n){        if(n==1||n==2)            return 1;        else            return f(n-1)+f(n-2);    }    //非递归的方法//   public static void main(String[] args) {//          System.out.println(funt(5));//   } //   public static int funt(int index) {//          if(index == 1 || index == 2) {//              return 1;//          }//          //          int f1 = 0;//          int f2 = 1;//          int res = 0 ;//          /*在这里因为第一位是写死的,所以会多算一次*///          for(int i=0; i<index -1 ;i++) {//              res = f1 + f2;//              f1 = f2;//              f2 = res;            //          }//          return res;//      }//    }}

4.冒泡排序

package algorithm;public class BubbleSort {    /*     * 思路:     * {12,30,20,10,9,13}     * 将第一个数依次与后面的数进行比较,找出最大的数,放在第一位    {30,{12,20,10,9,13}}     * 将剩下的数看作新的数组,再取第一个数与后面的进行比较,找出最大的    {30,20,{12,10,9,13}}     * 以此类推  得出最终的排序结果    {30,20,13,12,10,9}     */    public static int[] sort(int[] a){        for(int i=0;i<a.length;i++){            int temp=a[i];            for(int j=i+1;j<a.length;j++){                if(a[j]>temp){//大的在上面 ;a[j]<temp小的在上面                    a[i]=a[j];                    a[j]=temp;                    temp=a[i];                }            }        }        return a;    }    public static void main(String[] args) {        int[] a={11,81,27,53,42,45,6,77,178,9,70};        sort(a);        for(int i:a){            System.out.println(i+" ");        }    }}

5.快速排序

package algorithm;public class QuickSort {    /*     * 思路:     * {12302010913}     * 1.可以取第一个数作为middle(中值)       middle=12     * 2.比middle小的放在左边,比middle大的放在右边           {{10,9},12,{30,20,13}}     * 3.将左右两边的数组再用递归的方法同样的排序    middle1=10,middle2=30  {9,10,12,{20,13},30}     * 4.同样的方法再进行排序  middle3=20   {9,10,12,13,20,30}     */    public static int middle(int[] array,int left,int right){        int temp=array[left];        while(left!=right){            while(right>left&&array[right]>temp)                right--;            array[left]=array[right];            while(right>left&&array[left]>temp)                left++;            array[right]=array[left];        }        array[right]=temp;        return right;    }    public static int[] sort(int[] a,int left,int right){        if(left<right){            int i=middle(a,left,right);            sort(a,left,i);            sort(a,i+1,right);        }        return a;    }    public static void main(String[] args) {        int[] a={11,81,27,53,42,45,6,77,178,9,70};        sort(a,0,a.length-1);        for(int i:a){            System.out.print(i+" ");        }    }}