Elasticsearch_7_基本操作
This commit is contained in:
24
code/Java/sorting-algorithm/pom.xml
Normal file
24
code/Java/sorting-algorithm/pom.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>sorting-algorithm</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
<target>8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
26
code/Java/sorting-algorithm/src/main/java/BubbleSort.java
Normal file
26
code/Java/sorting-algorithm/src/main/java/BubbleSort.java
Normal file
@ -0,0 +1,26 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 冒泡排序
|
||||
*/
|
||||
public class BubbleSort {
|
||||
|
||||
public static int[] sort(int[] sourceArray) {
|
||||
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
boolean flag = true;
|
||||
for (int j = 0; j < arr.length - 1; j++) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
int tmp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = tmp;
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
23
code/Java/sorting-algorithm/src/main/java/InsertSort.java
Normal file
23
code/Java/sorting-algorithm/src/main/java/InsertSort.java
Normal file
@ -0,0 +1,23 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 插入排序
|
||||
*/
|
||||
public class InsertSort {
|
||||
|
||||
public static int[] sort(int[] sourceArray) {
|
||||
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int tmp = arr[i];
|
||||
int j = i;
|
||||
while (j > 0 && arr[j - 1] > tmp) {
|
||||
arr[j] = arr[j - 1];
|
||||
j--;
|
||||
}
|
||||
if (i != j) {
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
25
code/Java/sorting-algorithm/src/main/java/SelectionSort.java
Normal file
25
code/Java/sorting-algorithm/src/main/java/SelectionSort.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 选择排序
|
||||
*/
|
||||
public class SelectionSort {
|
||||
|
||||
public static int[] sort(int[] sourceArray) {
|
||||
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int min = i;
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
if (arr[j] < arr[min]) {
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
if (min != i) {
|
||||
int tmp = arr[i];
|
||||
arr[i] = arr[min];
|
||||
arr[min] = tmp;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
11
code/Java/sorting-algorithm/src/main/java/ZTest.java
Normal file
11
code/Java/sorting-algorithm/src/main/java/ZTest.java
Normal file
@ -0,0 +1,11 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
int[] source = {1, 4, 2, 5, 3};
|
||||
int[] sort01 = BubbleSort.sort(source);
|
||||
int[] sort02 = SelectionSort.sort(source);
|
||||
int[] sort03 = InsertSort.sort(source);
|
||||
System.out.println(Arrays.toString(sort03));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user