Skip to content

Commit d5c1a5b

Browse files
author
tianqing.liang
committed
数据结构与算法
1 parent 0cfc4ac commit d5c1a5b

File tree

17 files changed

+1035
-2
lines changed

17 files changed

+1035
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Mobile Tools for Java (J2ME)
1111
.mtj.tmp/
12+
.idea/*
1213

1314
# Package Files #
1415
*.jar

README.en.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# AlgorithmAndDataArchitecture
2+
3+
#### Description
4+
算法与数据结构
5+
6+
#### Software Architecture
7+
Software architecture description
8+
9+
#### Installation
10+
11+
1. xxxx
12+
2. xxxx
13+
3. xxxx
14+
15+
#### Instructions
16+
17+
1. xxxx
18+
2. xxxx
19+
3. xxxx
20+
21+
#### Contribution
22+
23+
1. Fork the repository
24+
2. Create Feat_xxx branch
25+
3. Commit your code
26+
4. Create Pull Request
27+
28+
29+
#### Gitee Feature
30+
31+
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
32+
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
33+
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
34+
4. The most valuable open source project [GVP](https://gitee.com/gvp)
35+
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
36+
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)

README.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
# DataStructure
2-
数据结构
1+
# AlgorithmAndDataArchitecture
2+
3+
#### 介绍
4+
算法与数据结构
5+
6+
#### 内容介绍
7+
1. 线性查找
8+
2. 选择排序
9+
3. 插入排序
10+
4. 数组
11+
5. 栈和队列
12+
6. 链表
13+

pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.skyl</groupId>
8+
<artifactId>algorithm</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>algorithm</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.7</maven.compiler.source>
18+
<maven.compiler.target>1.7</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
32+
<plugins>
33+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
34+
<plugin>
35+
<artifactId>maven-clean-plugin</artifactId>
36+
<version>3.1.0</version>
37+
</plugin>
38+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
39+
<plugin>
40+
<artifactId>maven-resources-plugin</artifactId>
41+
<version>3.0.2</version>
42+
</plugin>
43+
<plugin>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>3.8.0</version>
46+
</plugin>
47+
<plugin>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.22.1</version>
50+
</plugin>
51+
<plugin>
52+
<artifactId>maven-jar-plugin</artifactId>
53+
<version>3.0.2</version>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-install-plugin</artifactId>
57+
<version>2.5.2</version>
58+
</plugin>
59+
<plugin>
60+
<artifactId>maven-deploy-plugin</artifactId>
61+
<version>2.8.2</version>
62+
</plugin>
63+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
64+
<plugin>
65+
<artifactId>maven-site-plugin</artifactId>
66+
<version>3.7.1</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-project-info-reports-plugin</artifactId>
70+
<version>3.0.0</version>
71+
</plugin>
72+
</plugins>
73+
</pluginManagement>
74+
</build>
75+
</project>

src/main/java/com/skyl/App.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.skyl;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.skyl.Array04;
2+
3+
4+
/**
5+
* 泛型数组,可自动扩容
6+
* @param <E>
7+
*/
8+
public class Array<E> {
9+
10+
private E[] data;
11+
private int size;
12+
13+
// 构造函数,传入数组的容量capacity构造Array
14+
public Array(int capacity){
15+
data = (E[])new Object[capacity];
16+
size = 0;
17+
}
18+
19+
// 无参数的构造函数,默认数组的容量capacity=10
20+
public Array(){
21+
this(10);
22+
}
23+
24+
// 获取数组的容量
25+
public int getCapacity(){
26+
return data.length;
27+
}
28+
29+
// 获取数组中的元素个数
30+
public int getSize(){
31+
return size;
32+
}
33+
34+
// 返回数组是否为空
35+
public boolean isEmpty(){
36+
return size == 0;
37+
}
38+
39+
// 在index索引的位置插入一个新元素e
40+
public void add(int index, E e){
41+
42+
if(index < 0 || index > size)
43+
throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
44+
45+
if(size == data.length)
46+
resize(2 * data.length);
47+
48+
for(int i = size - 1; i >= index ; i --)
49+
data[i + 1] = data[i];
50+
51+
data[index] = e;
52+
53+
size ++;
54+
}
55+
56+
// 向所有元素后添加一个新元素
57+
public void addLast(E e){
58+
add(size, e);
59+
}
60+
61+
// 在所有元素前添加一个新元素
62+
public void addFirst(E e){
63+
add(0, e);
64+
}
65+
66+
// 获取index索引位置的元素
67+
public E get(int index){
68+
if(index < 0 || index >= size)
69+
throw new IllegalArgumentException("Get failed. Index is illegal.");
70+
return data[index];
71+
}
72+
73+
// 修改index索引位置的元素为e
74+
public void set(int index, E e){
75+
if(index < 0 || index >= size)
76+
throw new IllegalArgumentException("Set failed. Index is illegal.");
77+
data[index] = e;
78+
}
79+
80+
// 查找数组中是否有元素e
81+
public boolean contains(E e){
82+
for(int i = 0 ; i < size ; i ++){
83+
if(data[i].equals(e))
84+
return true;
85+
}
86+
return false;
87+
}
88+
89+
// 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
90+
public int find(E e){
91+
for(int i = 0 ; i < size ; i ++){
92+
if(data[i].equals(e))
93+
return i;
94+
}
95+
return -1;
96+
}
97+
98+
// 从数组中删除index位置的元素, 返回删除的元素
99+
public E remove(int index){
100+
if(index < 0 || index >= size)
101+
throw new IllegalArgumentException("Remove failed. Index is illegal.");
102+
103+
E ret = data[index];
104+
for(int i = index + 1 ; i < size ; i ++)
105+
data[i - 1] = data[i];
106+
size --;
107+
data[size] = null; // loitering objects != memory leak
108+
109+
if(size == data.length / 2)
110+
resize(data.length / 2);
111+
return ret;
112+
}
113+
114+
// 从数组中删除第一个元素, 返回删除的元素
115+
public E removeFirst(){
116+
return remove(0);
117+
}
118+
119+
// 从数组中删除最后一个元素, 返回删除的元素
120+
public E removeLast(){
121+
return remove(size - 1);
122+
}
123+
124+
// 从数组中删除元素e
125+
public void removeElement(E e){
126+
int index = find(e);
127+
if(index != -1)
128+
remove(index);
129+
}
130+
131+
@Override
132+
public String toString(){
133+
134+
StringBuilder res = new StringBuilder();
135+
res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
136+
res.append('[');
137+
for(int i = 0 ; i < size ; i ++){
138+
res.append(data[i]);
139+
if(i != size - 1)
140+
res.append(", ");
141+
}
142+
res.append(']');
143+
return res.toString();
144+
}
145+
146+
// 将数组空间的容量变成newCapacity大小
147+
private void resize(int newCapacity){
148+
149+
E[] newData = (E[])new Object[newCapacity];
150+
for(int i = 0 ; i < size ; i ++)
151+
newData[i] = data[i];
152+
data = newData;
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.skyl.InsertionSort03;
2+
3+
/**
4+
* 插入排序
5+
* 仅向已经排序好的前面数组按大小进行对应位置的插入
6+
*/
7+
public class InsertionSort {
8+
private InsertionSort(){}
9+
10+
//传统插入排序
11+
public static <E extends Comparable<E>> void sort(E[] arr){
12+
13+
for(int i = 0; i < arr.length; i ++){
14+
15+
// 将 arr[i] 插入到合适的位置
16+
E t = arr[i];
17+
int j;
18+
for(j = i; j - 1 >= 0 && t.compareTo(arr[j - 1]) < 0; j --){
19+
arr[j] = arr[j - 1];
20+
}
21+
arr[j] = t;
22+
}
23+
}
24+
25+
// 换个方法实现插入排序法,我们叫 sort2
26+
// 减少数据交换
27+
public static <E extends Comparable<E>> void sort2(E[] arr){
28+
29+
for(int i = arr.length - 1; i >= 0; i --){
30+
31+
// 将 arr[i] 插入到合适的位置
32+
E t = arr[i];
33+
int j;
34+
for(j = i; j + 1 < arr.length && t.compareTo(arr[j + 1]) > 0; j ++){
35+
arr[j] = arr[j + 1];
36+
}
37+
arr[j] = t;
38+
}
39+
}
40+
41+
private static <E extends Comparable<E>> boolean isSorted(E[] arr){
42+
43+
for(int i = 1; i < arr.length; i ++)
44+
if(arr[i - 1].compareTo(arr[i]) > 0)
45+
return false;
46+
return true;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.skyl.LinearSearch01;
2+
3+
/**
4+
* 使用泛型的线性查找
5+
*/
6+
public class LinearSearch {
7+
8+
private LinearSearch(){}
9+
10+
public static <E> int search(E[] data, E target){
11+
12+
for(int i = 0; i < data.length; i ++)
13+
if(data[i].equals(target))
14+
return i;
15+
16+
return -1;
17+
}
18+
19+
public static void main(String[] args){
20+
21+
Integer[] data = {24, 18, 12, 9, 16, 66, 32, 4};
22+
23+
int res = LinearSearch.search(data, 16);
24+
System.out.println(res);
25+
26+
int res2 = LinearSearch.search(data, 666);
27+
System.out.println(res2);
28+
}
29+
}

0 commit comments

Comments
 (0)