Java随笔(6):数据结构+算法(持续更新)

来源:互联网 发布:txt电子书软件下载 编辑:程序博客网 时间:2024/05/16 15:52

转载请注意:http://blog.csdn.net/wjzj000/article/details/74159490

本菜开源的一个自己写的Demo,希望能给Androider们有所帮助,水平有限,见谅见谅…
https://github.com/zhiaixinyang/PersonalCollect (拆解GitHub上的优秀框架于一体,全部拆离不含任何额外的库导入)
https://github.com/zhiaixinyang/MyFirstApp(Retrofit+RxJava+MVP)


排序

冒泡排序

思想:相邻的俩俩比较,根据二者的大小以及排列顺序决定交换移位问题。例如从小到大排列,那么相邻二者比较后大的那个数交互到后边,然后它再继续和下一个进行比较。和最后一个比较完毕后,最大的数便比较出来了。然后进行第二轮,第三轮….

    //从小到大    private static void bubbleSort(int[] a){        for(int i=0;i<a.length-1;i++){            for(int j=0;j<a.length-i-1;j++){                if(a[j]>a[j+1]){                    int temp=a[j+1];                    a[j+1]=a[j];                    a[j]=temp;                }            }        }    }

选择排序

思想:例如从大到小排列。先用一个变量记录为最小值,依次和这个最小值进行比较,一轮过后,这个变量将被赋值为最小,然后与最左边的位置进行交换…然后第二轮,第三轮…

    private static void selectSort(int[] a){        //声明移动位置的变量        int locationIn,locationMin,locationOut;         for(locationOut=0;locationOut<a.length;locationOut++){            //先给min赋值为要交换的位置            locationMin=locationOut;            for(locationIn=locationOut+1;locationIn<a.length;locationIn++){                if(a[locationIn]<a[locationMin]){                    //赋值当前最小的位置                    locationMin=locationIn;                }            }            //此时最小的数的位置已经确定,进行交换            int temp=a[locationOut];            a[locationOut]=a[locationMin];            a[locationMin]=temp;        }    } 

插入排序

思路:例如:从小到大排列。设置一个标识数,标识数的左边已经排列完毕。然后依次将将标识数出入到前面局部排列完毕的数组中,并依次后移标识数…

    private static void insertSort(int[] a){        int locationIn = 0, locationTag;        //默认时,第二个数为表示数,第一个数为局部排列数组...        for(locationTag = 1; locationTag < a.length ; locationTag++){            //将标识数临时赋值,用于插入到局部排列数组中            int temp = a[locationTag];            locationIn = locationTag;            //将标识数插入到局部排列数组中            while( locationIn>0 && a[locationIn-1]>=temp ){                a[locationIn] = a[locationIn-1];                --locationIn;            }            a[locationIn] = temp;        }    } 

比较简单的一种数据结构,先进后出。

//简单的实现一个public class Stack {    private int maxSize;    private int[] stackArray;    private int top;    public Stack(int size){        maxSize=size;        stackArray=new int[maxSize];        top=-1;    }    public void push(int num){        //简单起见:如果大于构造方法的初始数组值,不做处理        if(top+1<maxSize){            stackArray[++top]=num;        }    }    public int pop(){        if(top>-1){            return stackArray[top--];        }else{            //此处如果栈为空,抛异常            throw new EmptyStackException();        }    }}
  • 栈的实践场景
    • 字符串的逆转
    • 分隔符匹配
    • 实现加减乘除混合运算
      中缀表达:正常顺序
      后缀表达:从左到右,如果遇到数字,直接输出在表达式中,如果是符号按优先级进栈。/大于*大于+大于-,如果当前符号优先级大于栈顶,直接压如栈中,如果小于,依次出栈直到小于栈顶位置。如果是(直接压栈。如果是)。(上边的全部出栈并清除(。循环往复
      后缀使用:数字出栈,遇到符号,即为:后出的计算先出的。

队列

类似于排队的一种数据结构,先进先出。
LinkedList实现了Queue(Dqeue)接口,也就是说它可以进行队列的操作。
ArrayDqeue同样如此,拥有双端队列的能力。
详细使用见:http://www.yiibai.com/java/util/java_util_arraydeque.html


链表

我们知道如果单纯使用数组时,我们拥有很快的查找速度,但是插入会很慢,因为我们需要大量的移动操作。例如,我们要把一个数插入数据的中间,那么这个位置后边的数都要往后移。
为了应对这种情况,链表这种数据就应用而生。
LinkedList就是一种链表,因此它比ArrayList拥有更快的插入速度。这里简单的分析一下它。

    //内部类:链表类    private static class Node<E> {        E item;        Node<E> next;        Node<E> prev;        Node(Node<E> prev, E element, Node<E> next) {            this.item = element;            this.next = next;            this.prev = prev;        }    }    //添加时的add方法    public boolean add(E e) {        linkLast(e);        return true;    }    void linkLast(E e) {        final Node<E> l = last;        final Node<E> newNode = new Node<>(l, e, null);        last = newNode;        if (l == null)            first = newNode;        else            l.next = newNode;        size++;        modCount++;    }    //删除时的remove方法    public boolean remove(Object o) {        if (o == null) {            for (Node<E> x = first; x != null; x = x.next) {                if (x.item == null) {                    unlink(x);                    return true;                }            }        } else {            for (Node<E> x = first; x != null; x = x.next) {                if (o.equals(x.item)) {                    unlink(x);                    return true;                }            }        }        return false;    }    //取数据的方法    public E get(int index) {        //检查一下是否数组越界        checkElementIndex(index);        //通过node方法确定要查看的Node        return node(index).item;    }        private void checkElementIndex(int index) {        if (!isElementIndex(index))            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));    }    private boolean isElementIndex(int index) {        return index >= 0 && index < size;    }    //node方法    Node<E> node(int index) {        if (index < (size >> 1)) {            Node<E> x = first;            for (int i = 0; i < index; i++)                x = x.next;            return x;        } else {            Node<E> x = last;            for (int i = size - 1; i > index; i--)                x = x.prev;            return x;        }    }

给HashMap排序

    private static HashMap<Integer,String> mapSort(HashMap<Integer,String> map){        Set<Entry<Integer,String>> set=map.entrySet();        List<Entry<Integer,String>> list=new ArrayList<Entry<Integer,String>>(set);        Collections.sort(list,new Comparator<Entry<Integer,String>>(){            @Override            public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) {                // 如果o1小于o2,返回一个负数;如果o1大于o2,返回一个正数;如果他们相等,则返回0;                return o1.getKey()-o2.getKey();            }        });        HashMap<Integer,String> hashMap=new HashMap<Integer,String>();        for(Entry<Integer,String> entry:set){            hashMap.put(entry.getKey(), entry.getValue());        }        return hashMap;    }

尾声

最后希望各位看官可以star我的GitHub,三叩九拜,满地打滚求star:
https://github.com/zhiaixinyang/PersonalCollect
https://github.com/zhiaixinyang/MyFirstApp

原创粉丝点击