java——时间复杂度、动态数组

O(n)不一定小于O(n^2),要具体来看,而我们说的这种时间复杂度其实是渐进时间复杂度,描述的是n趋近于无穷的情况。

动态数组的时间复杂度:

  添加操作:O(n) addLast()的均摊复杂度为O(1)

  删除操作:O(n)

  修改操作:已知索引:O(1) 未知索引:O(n)

  查找操作:已知索引:O(1) 未知索引:O(n)

复杂度震荡:removeLast时resize过于着急(Eager)

解决方案:Lazy

 

实现动态数组:

public class Array<E> {
    //叫它静态数组
    //private int[] data;
    private E[] data;
    private int size;
    //构造函数
    public Array(int capacity) {
        data = (E[])new Object[capacity];
        size = 0;
    }
    //无参数的构造函数,默认数组的容量为10
    public Array() {
        this(10);
    }

    public Array(E[] arr) {
      //java不支持泛型数组:data = new E[];
      data = (E[]) new Object[arr.length];
      for(int i = 0 ; i < arr.length ; i ++)
        data[i] = arr[i];
      size = arr.length;
    }

public int getSize() {
        return size;
    }
    public int getCapacity() {
        return data.length;
    }
    // O(1)
    public void addLast(E e) {
        add(size, e);
    }
    // O(n)
    public void addFirst(E e) {
        add(0, e);
    }
    // O(n/2) = O(n)
    public void add(int index, E e) {
        if(size>=data.length)
            resize(2 *data.length);
        if(index<0 || index>size)
            throw new IllegalArgumentException("Add failed.index is error.");
        for(int i=size-1;i>=index;i--) {
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
        res.append("[");
        for(int i = 0 ; i<size ; i++) {
            res.append(data[i]);
            if(i != size - 1)
                res.append(", ");
        }
        res.append("]");
        return res.toString();
    }
    E get(int index) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        return data[index];
    }
    void set(int index, E e) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        data[index] = e;
    }
    public boolean contains(E e) {
        for(int i = 0; i < size; i++) {
            if(data[i].equals(e))
                return true;
        }
        return false;
    }
    public int find(E e) {
        for(int i = 0; i < size; i++) {
            if(data[i].equals(e))
                return i;
        }
        return -1;
    }
    public E remove(int index) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal");
        E res = data[index];
        for(int i = index; i<size; i++) {
            data[i] = data[i+1];
        }
        size--;
        //释放空间,也可以不写 
        //loitering objects != memory leak
        data[size] = null;
        if(size == data.length / 4 && data.length / 2 != 0)
            resize(data.length / 2);
        return res;
    }
    public E removeFirst() {
        return remove(0);
    }
    public E removeLast() {
        return remove(size-1);
    }
    //只删除了一个e,并不能保证删除了全部e
    public void removeElement(E e) {
        int index = find(e);
        if(index != -1)
            remove(index);
    }
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for(int i=0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }
}

 

posted @ 2018-10-14 21:43  高圈圈  阅读(812)  评论(0编辑  收藏  举报