JAVA architect of Advanced Data Structures and Algorithms programmer practicing internal strength

Hello everyone I am makasa
this column does, I will follow through before I recorded a video to learn the notes.
At the same time, by writing this blog to consolidate several of my foundation

Data Structures and Algorithms, by definition, is divided into two parts: data structures, algorithms. What is it they outlined their specific. Let us look at the following two graphs, plain and simple. Here you can probably understand less.


Here we focus on in terms of the linear structure.
First, the linear structure is divided into:
1. Linear sequential storage structure stored:
① array (preferably to a maximum length of -1, the length of the array can not be changed)
how to solve the array length is invariable: Create a new array, the length of the original array +1 (added) or -1 (deleted)
② :( last-out stack) Example: bullet clip
③ queue (FIFO) Example: ticket line
2. the linear structure is stored in a linked list stored:

① single list:

② circular list

③ doubly linked list


Then, over here, we focus to mention the array inside the search algorithm
search algorithm include:
① linear search: that is the definition of an array, is traversed find elements and array elements are equal to
② binary search (High Efficiency): The surface is not wide, because the requirements of the target array and orderly.

The following paste the code array (CRUD)

package cn.makasa;
import java.util.Arrays;

class MyArray {public
// define an array
Private int [] Elements;
// Constructors
public MyArray () {
Elements = new new int [0];
}
// Returns the length of the array
public int size () {
return elements.length;
}

/ **
* a, add an element to the end of the array
* /
public void the Add (int Element) {
// definition of a new array. 1: length of the original length of the array + 1'd
int [] = newArray new new int [elements.length + 1];
// iterate the original array 2, the value of the original array is assigned to the new array
for (int I = 0; I <elements.length; I ++) {
newArray [I] = Elements [I];
}
// 3. Add one end of an array element: additive element index is equal to the length of the array, i.e. the original:
newArray [elements.length] = element;
// assign the array. 4 of the new original array.
elements = newArray;
}

/ **
* Second, print all elements are displayed on the console
* /
public void Show () {
System.out.println (of Arrays.toString (Elements));
}

/ **
* three, remove array elements
* /
public Delete void (int index) {
// for out of bounds index is determined. 1.
iF (index <0 || index>-elements.length. 1) {
the throw a RuntimeException new new ( "subscript out of range");
}
// 2 defines a the new array length of the original length of the array -1
int [] = newArray new new int [-elements.length. 1];
//. 3. Traversing the new array, if the element before deleting elements: direct assignment element if after deleting elements: value plus. 1
for (int I = 0; I <newArray.length; I ++) {
IF (I <index) {
newArray [ I] = Elements [I];
} {the else
newArray [I] Elements = [I +. 1];
}
}
.. 4 // assign the array of the new to the old array
elements = newArray;
}

/ **
* IV to give each element
* /
public int GET (int index) {
return Elements [index];
}

/ **
* V, the element is inserted into a specified position
* /

INSERT void public (int index, int Element) {
. //. 1 define a new array length of the original length of the array + 1'd
int [] = newArr new new int [elements.length +. 1];
. 2 // iterate the original array, Analyzing if i <index direct assignment if the new array
for (int I = 0; I <elements.length; I ++) {
IF (i <index) {
newArr [I] = Elements [I];
} {the else
newArr [I + . 1] = Elements [I];
}
}
newArr [index] = Element;
// replace the original array
Elements = newArr;
}


// Replace element at the specified
public void SET (int index, int Element) {
Elements [index] = Element;
}

/**
* 六、线性查找
*/
public int search(int target) {
for(int i =0;i<elements.length;i++){
if(elements[i] == target){
return i;
}
}
return -1;
}

/ **
* seven binary search
* /
public int binarySearch (int target) {
int the begin = 0;
int = End-elements.length. 1;
int = MID (the begin End +) / 2;
int index = -1;
the while (to true) {
// this element is not under what circumstances?
// If the start position after the end of or coincident, without this element
IF (the begin> = end) {
return -1;
}
IF (elements [MID] == target) {
return MID;
} the else {
IF (Elements [MID]> target) {
End = MID-. 1;
} the else {
the begin = MID +. 1;
}
MID = (the begin + End) / 2;
}
}
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130.
131 is
132
test classes:

makasaTest package;

import cn.makasa.MyArray;

{class testArray public
public static void main (String [] args) {
// create an array of objects
MyArray MyArray new new mA = ();
// Get array length
int size = ma.size ();
System.out.println ( "Original array of length "+ size);

// all elements
ma.show ();

// add elements
ma.add (. 9);
ma.add (. 8);
ma.add (. 7);
ma.show ();

// delete an element
ma.delete (1);
ma.show ();
System.out.println (ma.get (1));
System.out.println ( "========== ==== ");

// insert an element
ma.insert (0,100);
ma.show ();

// modify elements
ma.set (2,100);
ma.show ();
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
Output Results:

Source array of length 0
[]
[9, 8, 7]
[9, 7]
7

============
[100, 9, 7]
[100, 9, 100]

Guess you like

Origin www.cnblogs.com/java188/p/12129065.html