# Data Structure and Algorithms - Overview, Dynamic Array (Initial Knowledge)

Keywords: less Java

## data structure

• Logical structure

• Collection structure: Data elements in a collection structure have no other relationship with each other except that they belong to the same collection.

• Linear structure: A one-to-one relationship between data elements in a linear structure

• Tree structure: A one-to-many hierarchical relationship between elements in a tree structure

• Graphic Structure: Data Elements of Graphic Structure Are Many-to-Many Relations

• Physical structure - refers to the logical structure of data stored in a computer

• Sequential storage structure: Open up a set of space to store data in domestic demand, which is usually realized by arrays. The space itself in the array is continuous, which ensures the relationship between data.

• Chain Storage Architecture: Open up a group of random space to store data, usually implemented by nodes. Nodes not only store data, but also store the location of the next node to ensure the relationship between data.

Note:

```Sequential Storage Structure Check Fast, Add, Delete Slowly
Chain Storage Architecture Checking Slowly, Increasing and Deleting Quickly
```

## algorithm

• What is an algorithm?

Algorithms are steps to solve a problem.

• How to evaluate an algorithm?

Post-event statistical method: This method is mainly to determine the efficiency of the algorithm by comparing the running time of different algorithm programs with the calculated u-age timer through the designed programs and data.
This method has many shortcomings: 1. Programs must be programmed in advance, and then run. If the amount of data processed is large, it will take a lot of time and energy; 2. The main decisions and dependencies are hardware and software; 3. The design of test data for algorithms is difficult, and it will take a lot of time when the amount of data is large.
Event Analysis and Estimation Method: This method mainly estimates the algorithm according to statistical method before compiling the computer program.

• Time complexity of algorithm

• Constant order O(1)1 denotes that it does not increase with the increase of N.

• Linear order O(n) ignores constants and coefficients such as 2n+3.

• Logarithmic O(logn)

• Square order O(n^2)

## Dynamic arrays (dynamic arrays are concrete implementations of sequential storage structures)

• The Java built-in array features:

Each storage space is uniform in size and continuous in address
Once the length is determined, it cannot be changed.
Only the same type of data can be stored
Provide corner access elements

• Expansion: First create a new array, then copy the original array, and replace the original data with new data.
```//Judge whether it is full
if(size==data.length) {
resize(2*data.length);
}
for(int i=size-1;i>=index;i--) {
data[i+1]=data[i];
}
data[index]=e;
size++;

```
• Shrinkage: First create a new array, then copy the original array, and replace the original data with new data.
(Conditions: 1. The default capacity should not be reduced to a minimum; 2. The number of effective elements is less than 1/4 of the capacity.)
```if(data.length>DEFAULT_SIZE&&size<=data.length/4) {
resize(data.length/2);
}
```
``` /**
* Changing the length of data (expansion, contraction)
* @param newLen
*/
private void resize(int newLen) {
// TODO Auto-generated method stub
E[] newData=(E[]) new Object[newLen];
/*for(int i=0;i<data.length;i++) {*/
for(int i=0;i<size;i++) {
newData[i]=data[i];
}
data=newData;
}
```
• How to encapsulate:

• Attributes:

Number of valid elements in an int size array
Maximum capacity data.length of an int capacity array
Storage container for E [] data array (E generic)

• Behavior: add, delete, modify, check, other

## Sequential storage structure of linear tables

• What is a linear table?
Finite Sequence of Zero or More Data Elements

• Linear Table Interface List(List is the ultimate parent interface of a linear table)

```public interface List<E> {
/**
* Get the number of elements in a linear table (the number of linear tables)
* @return Number of effective elements in linear table
*/
public int getSize();
/**
* Judging whether the table is empty
* @return Whether the Boolean type value is empty or not
*/
public boolean isEmpty();
/**
* Add element e to the inddex corner label specified in the linearity table
* @param index Specifies the effective length of a linear table with angle label 0<=index<=size
* @param e Elements to insert
*/
/**
* Header insertion elements for linearity
* @param e The element to be inserted is set at corner zero.
*/
/**
* Insert elements at the end of a table in a linear form
* @param e The element to be inserted is at the corner size
*/
/**
* Get the element at the specified index corner in the linearity table
* @param index Specified corner label 0<=size<size
* @return The element corresponding to the corner label
*
*/

public E get(int index);
/**
* Get the elements of the header in a linear table
* @return Table header element index=0
*/
public E getFirst();
/**
* Getting the elements at the end of a linear table
* @return The element index at the end of the table = size-1
*/
public E getLast();
/**
* Modify the elements specified at index in the linear table to all new elements
* @param index Designated corners
* @param e element fresh
*/
public void set(int index ,E e);
/**
* Determine whether the linear table contains the specified element e by default
* @param e To determine whether elements exist or not
* @return  Boolean Type Value of Element Existence
*/
public boolean contains(E e);
/**
* The cursor for obtaining the specified element e in the linearity table defaults to looking after going
* @param e Data to be queried
* @return Elements (data) are the corners of linearity
*/
public int find(E e);
/**
* Delete the element at the specified corner label in the linearity table and return
* @param index Specified corner label 0<=index<size
* @return Remove old elements
*/
public E remove(int index);
/**
* Delete header elements of linear tables
*/
public E removeFirst();
/**
* Delete tail elements of linear tables
* @return Table tail element
*/
public E removeLast();
/**
* Delete the specified element e in the linearity table
* @param e
*/
public void removeElement(E e);
/**
* Clear Linear Table
*/
public void clear();
}

```
• Implementing List-Sequential Linear Table-Sequential Table with Sequential Storage Structure
```public class ArrayList<E> implements List<E> {
private E[] data; //Containers for storing data elements
private int size; //The Number of Valid Elements in Linear Tables Data. Maximum capacity of Linear Tables
private static int DEFAULT_SIZE=10;//Default capacity of container
/**
* Create a linear table with a default capacity of 10
*/
public ArrayList() {
/*	this.data=(E[]) new Object[DEFAULT_SIZE];
this.size=0;*/
this(DEFAULT_SIZE);
}
/**
* Create a linear table with capacity specified
* @param capacity
*/
public ArrayList(int capacity) {
this.data=(E[]) new Object[capacity];
this.size=0;
}
/**
* Encapsulate an array into a linear table
* @param arr
*/
public ArrayList(E[] arr) {
// TODO
}

@Override
public int getSize() {
// TODO Auto-generated method stub
return size;
}

@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return size==0;
}

@Override
public void add(int index, E e) {
// TODO Auto-generated method stub
if(index<0||index>size) {
throw new ArrayIndexOutOfBoundsException("add Function Corner Crossing the Boundary");
}
//Judge whether it is full
if(size==data.length) {
resize(2*data.length);
}
for(int i=size-1;i>=index;i--) {
data[i+1]=data[i];
}
data[index]=e;
size++;
}
/**
* Changing the length of data (expansion, contraction)
* @param newLen
*/
private void resize(int newLen) {
// TODO Auto-generated method stub
E[] newData=(E[]) new Object[newLen];
/*for(int i=0;i<data.length;i++) {*/
for(int i=0;i<size;i++) {
newData[i]=data[i];
}
data=newData;
}
@Override
// TODO Auto-generated method stub
}

@Override
// TODO Auto-generated method stub
}

@Override
public E get(int index) {
// TODO Auto-generated method stub
if(index<0||index>size-1) {
throw new ArrayIndexOutOfBoundsException("get Function Corner Crossing the Boundary");
}
return data[index];
}

@Override
public E getFirst() {
// TODO Auto-generated method stub
return get(0);
}

@Override
public E getLast() {
// TODO Auto-generated method stub
return get(size-1);
}

@Override
public void set(int index, E e) {
// TODO Auto-generated method stub
if(index<0||index>size-1) {
throw new ArrayIndexOutOfBoundsException("set Function Corner Crossing the Boundary");
}
data[index]=e;
}

@Override
public boolean contains(E e) {
// TODO Auto-generated method stub

if(isEmpty()) {
return false;
}
for(int i=0;i<size;i++) {
if(data[i]==e) {
return true;
}
}
return false;
}

@Override
public int find(E e) {
// TODO Auto-generated method stub
if(isEmpty()) {
return -1;
}
for(int i=0;i<size;i++) {
if(data[i]==e) {
return i;
}
}
return -1;
}

@Override
public E remove(int index) {
// TODO Auto-generated method stub
if(index<0||index>size-1) {
throw new ArrayIndexOutOfBoundsException("remove Corner crossing");
}

E e=get(index);
for(int i=index+1;i<=size-1;i++) {
data[i-1]=data[i];
}
size--;

//Judging whether to shrink or not
//1. Minimum default capacity should not be reduced
//2. The number of effective elements is less than 1/4 of the capacity.
if(data.length>DEFAULT_SIZE&&size<=data.length/4) {
resize(data.length/2);
}

return e;
}

@Override
public E removeFirst() {
// TODO Auto-generated method stub
return remove(0);
}

@Override
public E removeLast() {
// TODO Auto-generated method stub
return remove(size-1);
}

@Override
public void removeElement(E e) {
// TODO Auto-generated method stub
int index=find(e);
if(index==-1) {
throw new IllegalArgumentException("Delete element does not exist");
}
remove(index);
}

@Override
public void clear() {
// TODO Auto-generated method stub
size=0;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder sb=new StringBuilder();
sb.append("ArrayList:size="+size+",capacity="+data.length+"\n");
if(isEmpty()) {
sb.append("[]");
}else {
sb.append('[');
for(int i=0;i<size;i++) {
sb.append(data[i]);
if(i==size-1) {
sb.append(']');

}else {
sb.append(',');
}
}
}

return sb.toString();

}
}
```
• Test class
```public class Main {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<Integer>();
System.out.println(list);

for(int i=1;i<=5;i++) {
}
System.out.println(list);

for(int i=6;i<=10;i++) {
}
System.out.println(list);
System.out.println(list);
for(int  i=11;i<=30;i++) {
}
System.out.println(list);

for(int i=0;i<=10;i++) {
System.out.println(list.removeFirst());
}
System.out.println(list);
for(int i=0;i<=10;i++) {
System.out.println(list.removeFirst());
}
System.out.println(list);

}
}
```

Test results:

Posted by Mikkki on Sat, 07 Sep 2019 04:01:14 -0700