20171015

来源:互联网 发布:苹果mac删除软件 编辑:程序博客网 时间:2024/06/10 03:48
package Text;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];    }}
package Text;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;    }}

冒泡排序

package sort;public class Bubble1 {    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[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.printf(i+" ");        }    }}

快速排序
算法思想:基于分治的思想,是冒泡排序的改进型。首先在数组中选择一个基准点(该基准点的选取可能影响快速排序的效率,后面讲解选取的方法),然后分别从数组的两端扫描数组,设两个指示标志(lo指向起始位置,hi指向末尾),首先从后半部分开始,如果发现有元素比该基准点的值小,就交换lo和hi位置的值,然后从前半部分开始扫秒,发现有元素大于基准点的值,就交换lo和hi位置的值,如此往复循环,直到lo>=hi,然后把基准点的值放到hi这个位置。一次排序就完成了。以后采用递归的方式分别对前半部分和后半部分排序,当前半部分和后半部分均有序时该数组就自然有序了。

package sort;public class quick {    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[left] = 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,right);            sort(a, left: i + 1,right);        }        return a;    }    public static void main(String[] args){        int[] a = {21,81,27,53,42,45,6,77,178,9,70};         sort(a, left:0,right:a.length - 1);        for(int i:a){            System.out.print(i+" ");        }    }}
原创粉丝点击