Skip to content

If we add a while loop, we're going to move the high order to the right. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ target/
.DS_Store
Thumbs.db
*.orig

.vs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<modules>
<module>stage-1</module>
<module>stage-2</module>
<module>stage-3</module>
<module>stage-4</module>
<module>stage-5</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>「一入 Java 深似海 」系列 :: 第二期 :: 第二节</name>
<name>「一入 Java 深似海 」系列 :: 第二期 :: 第三节</name>
<artifactId>stage-2-lesson-3</artifactId>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<name>「一入 Java 深似海 」系列 :: 第二期 :: 第三节</name>
<artifactId>stage-2-lesson-4</artifactId>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@ public void sort(T[] values) {
// Given array : [3,1,2,5,4]
// for 1 |
// for 2 |
for (int i = 0; i < size; i++) {
// 第 i 号元素
T t = values[i]; // 产生临时变量
for (int j = i + 1; j < size; j++) {
// 第 i 号元素与 i + 1 对比
if (t.compareTo(values[j]) == 1) { // 低位 > 高位
// 交换元素 [i + 1] = [i]
values[i] = values[j];
values[j] = t;
// [0] = 3 , [1] = 2
// [1] = [1](2) + [0](3) = 5
// [0] = [1](5) - [0](3) = 2
// [1] = [1](5) - [0](2) = 3
break;
int count = size - 1;
while ( count >= 0) {
for (int i = 0; i < size; i++) {
// 第 i 号元素
T t = values[i]; // 产生临时变量
for (int j = i + 1; j < size; j++) {
// 第 i 号元素与 i + 1 对比
if (t.compareTo(values[j]) == 1) { // 低位 > 高位
// 交换元素 [i + 1] = [i]
values[i] = values[j];
values[j] = t;
// [0] = 3 , [1] = 2
// [1] = [1](2) + [0](3) = 5
// [0] = [1](5) - [0](3) = 2
// [1] = [1](5) - [0](2) = 3
break;
}
}
}
count--;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.segmentfault.deep.in.java.collection.algorithm;

import java.util.stream.Stream;
import java.util.Arrays;

public class InsertionSort<T extends Comparable<T>> implements Sort<T> {

Expand All @@ -17,20 +17,30 @@ public void sort(T[] values) {
// [j = 0] = 3, [i = 1] = 1 , t = [i = 1] = 1
// [i = 1] = [j = 0] , [j = 0] = t = 1
T t = values[i]; // 产生临时变量
for (int j = i - 1; j >= 0; j--) {
if (t.compareTo(values[j]) < 1) { // 高位 < 低位
values[i] = values[j]; // 高位获取低位的值
values[j] = t; // 低位得到高位的值
}
int j = i;
while (j > 0 && t.compareTo(values[j - 1]) < 0) {
//往后移动让出插入空间
values[j] = values[j - 1];
j--;
}
//插入values[i]到对应位置
values[j] = t;
System.out.printf("第%d轮:%s\n", i, Arrays.toString(values));
}
}

public static void main(String[] args) {
Integer[] values = Sort.of(3, 1, 2, 5, 4);
Sort<Integer> sort = new InsertionSort<>(); // Java 7 Diamond 语法
System.out.println("一般情况");
Integer[] values = Sort.of(3, 2, 1, 5, 4);
Sort<Integer> sort = new InsertionSort<>();
sort.sort(values);
Stream.of(values).forEach(System.out::println);
System.out.println(Arrays.toString(values));

System.out.println("完全逆序");
values = Sort.of(5, 4, 3, 2, 1);
sort = new InsertionSort<>();
sort.sort(values);
System.out.println(Arrays.toString(values));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void merge(Comparable[] values, int low, int mid, int high) {
Comparable[] a2 = new Comparable[n2];

//把 values[0...mid] 内容复制给 a1
System.arraycopy(values, 0, a1, 0, n1);
System.arraycopy(values, low, a1, 0, n1);
//把 values[mid+1...high] 内容复制给 a2
System.arraycopy(values, mid + 1, a2, 0, n2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.segmentfault.deep.in.java.collection.algorithm;

import java.util.Arrays;
import java.util.stream.Stream;

public class QuickSort<T extends Comparable<T>> implements Sort<T> {
Expand Down Expand Up @@ -63,28 +64,28 @@ int partition(T[] values, int low, int high) {
// pIndex = 3

T pivot = values[high];
int i = low - 1;
int i = low;

for (int j = low; j < high; j++) {
if (values[j].compareTo(pivot) < 1) { // <=
i++; // -1 -> 0
T temp = values[i]; // 低位数据
values[i] = values[j]; // 低位数据获取高位数据
values[j] = temp;
i++; // -1 -> 0
}
}

T temp = values[i + 1];
values[i + 1] = values[high];
T temp = values[i];
values[i] = values[high];
values[high] = temp;

return i + 1; // 游标+1
return i;
}

public static void main(String[] args) {
Integer[] values = Sort.of(3, 1, 2, 5, 4);
Integer[] values = Sort.of(2, 5, 6, 7, 8, 8, 9, 2, 1, 6, 7, 5, 6, 11, 23);
Sort<Integer> sort = new QuickSort<>(); // Java 7 Diamond 语法
sort.sort(values);
Stream.of(values).forEach(System.out::println);
System.out.println(Arrays.asList(values));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<module>lesson-1</module>
<module>lesson-2</module>
<module>lesson-3</module>
<module>stage-2-lesson-4</module>
<module>lesson-4</module>
</modules>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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">
<parent>
<artifactId>deep-in-java</artifactId>
<groupId>com.segmentfault</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>stage-3</artifactId>
<name>「一入 Java 深似海 」系列 :: 第三期</name>
<modules>
<module>stage-3-lesson-1</module>
<module>stage-3-lesson-2</module>
<module>stage-3-lesson-3</module>
<module>stage-3-lesson-4</module>
</modules>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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">
<parent>
<artifactId>stage-3</artifactId>
<groupId>com.segmentfault</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>「一入 Java 深似海 」系列 :: 第三期 :: 第一节</name>
<artifactId>stage-3-lesson-1</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.segmentfault.deep.in.java.process;

import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

public class ChildProcessDemo {

public static void main(String[] args) throws IOException {

// IDEA(主进程) -> 启动 ChildProcessDemo -> Windows 计算器(calc)
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

if (operatingSystemMXBean.getName().startsWith("Windows")) {
// 启动计算器
Runtime.getRuntime().exec("calc");

// Process process = Runtime.getRuntime().exec("dir");
//
// InputStream inputStream = process.getInputStream();
// int data = 0;
// while ((data = inputStream.read()) > -1) {
// System.out.print(data);
// }
}

}
}
Loading