算法入门

来源:互联网 发布:windows7安装apache 编辑:程序博客网 时间:2024/05/16 14:14

java实现栈和队列

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];    }}
队列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.head = 0;        this.end = 0;        this.size = 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)//            return null;            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;    }}

Fibonacci

public class Fibonacci {    public static int f(int n){        if(n<=0)return 0;        if(n==1)return 1;        if(n==2)return 1;        return f(n-1)+f(n-2);    }    public static void main(String[] args) {        //1 1 2 3 5 8 13        //递归        System.out.println(f(8));        //非递归        int n=1;        int a=1;        int b=1;        while (n>2){            int tem=b;            b=a+b;            a=tem;            n--;        }        System.out.println(b);    }}

快速排序

public class QuickSort {    public static void sort(int a[], int low, int hight) {        int i, j, index;        if (low > hight) {            return;        }        i = low;        j = hight;        index = a[i]; // 用子表的第一个记录做基准        while (i < j) { // 从表的两端交替向中间扫描            while (i < j && a[j] >= index)                j--;            if (i < j)                a[i++] = a[j];// 用比基准小的记录替换低位记录            while (i < j && a[i] < index)                i++;            if (i < j) // 用比基准大的记录替换高位记录                a[j--] = a[i];        }        a[i] = index;// 将基准数值替换回 a[i]        sort(a, low, i - 1); // 对低子表进行递归排序        sort(a, i + 1, hight); // 对高子表进行递归排序    }    public static void quickSort(int a[]) {        sort(a, 0, a.length - 1);    }    public static void main(String[] args) {        int a[] = { 49, 38, 65, 97, 76, 13, 27, 49,5 };        quickSort(a);        System.out.println(Arrays.toString(a));    }}
原创粉丝点击