Linear learning table data structure (II)

Opening : The last time we talked, basic array of additions and deletions to change search. Now let's optimize and improve, and analyze.

1. First we define an array type does not support generics.

2. When capacity is not enough to use, you should be able to automatically trigger the expansion operation.

We look to achieve. We use the generic type T to represent.

public class Array<T>
    {
        private T[] Data;
        private int Size;
        private int Capacity;
        public bool IsFull => this.Capacity == this.Size;
        public Array(int capacity = 20)
        {
            this.Capacity = capacity;
            this.Data = new T[this.Capacity];
        }
}

At this time, the operation should CRUD generic variables T

Such as add, at this time we increase the expansion of operations, we have to add an array traversal copy operation, depending on the size of the capacity of the capacity size of the array should be given.

public void Add(T value)
        {
            if (IsFull)
                this.ExpandCapacity(this.Capacity * 2);
            this.Size++;
            this.Data[this.Size - 1] = value;
        }
}
private void ExpandCapacity(int capacity)
 {
        var newData = new T[capacity];
        for (int i = 0; i < newData.Length; i++)
        {
            newData[i] = this.Data[i];
        }
        this.Data = newData;
        this.Capacity = capacity;
 }

Deletion

public T Delete(int index)
{
        if (index < 0)
            throw new Exception("index is less than zero");
        if (index > this.Capacity-1)
            throw new Exception("index is more than capacity");
        if (index > this.Size-1)
            return default;
        var value = this.Data[index];
        for (int i = index; i < this.Size-1; i++)
        {
            this.Data[i] = this.Data[i+1];
        }
        this.SetEmpty(this.Size-1);
        this.Size--;
        if (this.Size <= this.Capacity/2)
           this.NarrowCapacity(this.Capacity / 2);
        return value;
 }

private void NarrowCapacity(int capacity)
{
        var newData = new T[capacity];
        for (int i = 0; i < this.Data.Length; i++)
        {
            newData[i] = this.Data[i];
        }
         this.Data = newData;
         this.Capacity = capacity;
}

 

At this time, search and other operations need to be compared to the rewritable type T Equals object base class () RULES

public bool IsContain(int value)
{
        var isContain = false;
        for (int i = 0; i < this.Size; i++)
        {
            if (this.Data[i].Equals(value))
                isContain = true;
        }
        return isContain;
}

 

At this point we look at the complexity of our algorithm

1. Increase the O (1) that have nothing to say, but if it is full, triggered the expansion of complexity is O (n).

2. Insert inserted, and if the index is inserted Size, at this time complexity is O (1), but the worst case to be inserted into a first, at this time complexity is O (n), average complexity to (n / 2), or O (n)

3. Delete to delete the same. We will have the worst-case complexity is O (n).

4. Find O (1) 

 

But there is a problem, for example, is filled to capacity at this time we added a trigger expansion, this time they remove the last, Size of the array at this time is half the capacity, but also shrink the container. Is O (1) operation at this time of complexity would be O (n), (complexity concussion), then we can change it. When the size is less than 1/4 of the container, shrinking the container before trigger. To avoid this and add and delete O (1) time complexity of the operation to bring the shock.

if (this.Size <= this.Capacity/4)
               this.NarrowCapacity(this.Capacity / 2);

Github following address my project needy students can self-created Oh Address:  https://github.com/GuanZhengXin/DataStruct

So we simply implement a basic array of structures, to learn about the next issue we stack structure and implementation.

Guess you like

Origin www.cnblogs.com/GuanZx/p/11037208.html