Skip to content

Commit b55f211

Browse files
committed
算法-十大经典排序算法
1 parent 14440ea commit b55f211

File tree

25 files changed

+2466
-6
lines changed

25 files changed

+2466
-6
lines changed

Diff for: Algorithm/.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### STS ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
15+
### IntelliJ IDEA ###
16+
.idea
17+
*.iws
18+
*.iml
19+
*.ipr
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/

Diff for: Algorithm/HELP.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Getting Started
2+
3+
### Reference Documentation
4+
For further reference, please consider the following sections:
5+
6+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.7.5/maven-plugin/reference/html/)
8+
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.7.5/maven-plugin/reference/html/#build-image)
9+
* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/2.7.5/reference/htmlsingle/#using.devtools)
10+

Diff for: Algorithm/pom.xml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.7.5</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.algorithm</groupId>
12+
<artifactId>Algorithm</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>Algorithm</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-devtools</artifactId>
28+
<scope>runtime</scope>
29+
<optional>true</optional>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
<configuration>
49+
<excludes>
50+
<exclude>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
</exclude>
54+
</excludes>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.algorithm;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* 算法
8+
*
9+
* @author wliduo[[email protected]]
10+
* @date 2022/11/22 9:52
11+
*/
12+
@SpringBootApplication
13+
public class AlgorithmApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(AlgorithmApplication.class, args);
17+
}
18+
19+
}
+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package com.algorithm.sort;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 冒泡排序
7+
*
8+
* 比较相邻的元素。如果第一个比第二个大(小),就交换他们两个
9+
* 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对
10+
* 这步做完后,最后的元素会是最大(小)的数
11+
* 针对所有的元素重复以上的步骤,除了最后一个
12+
*
13+
* 时间复杂度
14+
*
15+
* @author wliduo[[email protected]]
16+
* @date 2022/11/22 9:52
17+
*/
18+
public class BubbleSort {
19+
20+
/**
21+
* 冒泡排序算法
22+
*
23+
* @param args
24+
*/
25+
public static void main(String[] args) {
26+
int[] array = {5, 3, 2, 4, 9, 8, 7, 1, 6};
27+
28+
System.out.println("---------- bubbleSort1 ----------");
29+
bubbleSort1(Arrays.copyOf(array, array.length));
30+
31+
System.out.println("---------- bubbleSort2 ----------");
32+
bubbleSort2(Arrays.copyOf(array, array.length));
33+
34+
System.out.println("---------- bubbleSort3 ----------");
35+
bubbleSort3(Arrays.copyOf(array, array.length));
36+
37+
System.out.println("---------- bubbleSort4 ----------");
38+
int[] array4 = {4, 1, 2, 3, 9, 8, 7, 5, 6};
39+
bubbleSort4(Arrays.copyOf(array4, array.length));
40+
}
41+
42+
/**
43+
* 先写出一轮的冒泡排序处理,冒泡一样的交换位置到最后一位
44+
*
45+
* 大于就交换位置是从小到大,每次大于后面的值才交换,最后一位是最大值
46+
* 小于就交换位置是从大到小,每次小于后面的值才交换,最后一位是最小值
47+
*
48+
* array.length - 1,不然会超出边界,因为 array[j + 1]
49+
*
50+
* 当然也可以是从j = 1开始,j和j - 1进行比较,这样就不用array.length - 1了
51+
* 大于和小于也需要调换,大于会变成从大到小,小于会变成从小到大
52+
* 当然这种逆向的理解起来比较复杂一点,第二块代码是逆向的写法
53+
*
54+
* @param array
55+
*/
56+
private static void bubbleSort1(int[] array) {
57+
print(array);
58+
59+
for (int j = 0; j < array.length - 1; j++) {
60+
if (array[j] > array[j + 1]) {
61+
int temp = array[j];
62+
array[j] = array[j + 1];
63+
array[j + 1] = temp;
64+
}
65+
}
66+
67+
/*for (int j = 1; j < array.length; j++) {
68+
if (array[j] < array[j - 1]) {
69+
int temp = array[j];
70+
array[j] = array[j - 1];
71+
array[j - 1] = temp;
72+
}
73+
}*/
74+
75+
print(array);
76+
}
77+
78+
/**
79+
* 基于一轮的冒泡排序处理,再增加一个外循环
80+
* 将每个位置的数字都进行一轮冒泡交换
81+
* 这样一个最基础的冒泡排序就完成了
82+
*
83+
* 给定一个count记录循环次数
84+
*
85+
* @param array
86+
*/
87+
private static void bubbleSort2(int[] array) {
88+
print(array);
89+
90+
// count记录循环次数
91+
int count = 0;
92+
93+
for (int i = 0; i < array.length; i++) {
94+
for (int j = 0; j < array.length - 1; j++) {
95+
count++;
96+
if (array[j] > array[j + 1]) {
97+
int temp = array[j];
98+
array[j] = array[j + 1];
99+
array[j + 1] = temp;
100+
}
101+
}
102+
}
103+
System.out.println("count: " + count);
104+
105+
print(array);
106+
}
107+
108+
/**
109+
* 优化版本,内循环减去i就行了
110+
* 每次冒泡后到i位置的数字都已经排序好了
111+
* 所以就没必要重复排
112+
*
113+
* 可以看到count次数少了一半
114+
*
115+
* @param array
116+
*/
117+
private static void bubbleSort3(int[] array) {
118+
print(array);
119+
120+
// count记录循环次数
121+
int count = 0;
122+
123+
for (int i = 0; i < array.length; i++) {
124+
for (int j = 0; j < array.length - i - 1; j++) {
125+
if (array[j] > array[j + 1]) {
126+
int temp = array[j];
127+
array[j] = array[j + 1];
128+
array[j + 1] = temp;
129+
}
130+
count++;
131+
}
132+
}
133+
134+
// 还有一种外循环i反过来循环的,意思差不多,就是逆向的
135+
/*for (int i = array.length - 1; i > 0 ; i--) {
136+
for (int j = 0; j < i; j++) {
137+
count++;
138+
if (array[j] > array[j + 1]) {
139+
int temp = array[j];
140+
array[j] = array[j + 1];
141+
array[j + 1] = temp;
142+
}
143+
}
144+
}*/
145+
System.out.println("count: " + count);
146+
147+
print(array);
148+
}
149+
150+
/**
151+
* 优化版本v2,假定数组某些数已经有序
152+
* 在每轮交换处理可以定一个标记,如果这轮比较都没有交换位置的话
153+
* 说明数据已经有序,可以直接结束
154+
*
155+
* 给定一个count记录循环次数
156+
* 换一个存在某些数有序的数组,可以看到count次数又会变少
157+
*
158+
* @param array
159+
*/
160+
private static void bubbleSort4(int[] array) {
161+
print(array);
162+
163+
// count记录循环次数
164+
int count = 0;
165+
166+
for (int i = 0; i < array.length; i++) {
167+
// 设定一个标记
168+
boolean mark = Boolean.TRUE;
169+
170+
for (int j = 0; j < array.length - i - 1; j++) {
171+
if (array[j] > array[j + 1]) {
172+
int temp = array[j];
173+
array[j] = array[j + 1];
174+
array[j + 1] = temp;
175+
mark = Boolean.FALSE;
176+
}
177+
count++;
178+
}
179+
180+
// 若为true,则表示此次循环没有进行交换
181+
if (mark) {
182+
// 也就是待排序列已经有序,排序已经完成,直接结束
183+
break;
184+
}
185+
}
186+
187+
System.out.println("count: " + count);
188+
189+
print(array);
190+
}
191+
192+
/**
193+
* 打印数组
194+
*
195+
* @param array
196+
*/
197+
private static void print(int[] array) {
198+
for (int i = 0; i < array.length; i++) {
199+
System.out.print(array[i] + " ");
200+
}
201+
System.out.println();
202+
}
203+
204+
}

0 commit comments

Comments
 (0)