算法入门

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));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值