Skip to content
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

重写java归并排序 #39

Open
wants to merge 4 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
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ _book
*.pdf
\.idea/

# idea
*.iml

src/javaSortTest/target/
# elipse
.classpath
.project
.settings

# build
target
bin
63 changes: 34 additions & 29 deletions 5.mergeSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,47 +143,52 @@ func merge(left []int, right []int) []int {
public class MergeSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
public int[] sort(int[] arr) throws Exception {
mergeSort(arr, 0, arr.length - 1);
return arr;
}

if (arr.length < 2) {
return arr;
private void mergeSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int middle = (int) Math.floor(arr.length / 2);

int[] left = Arrays.copyOfRange(arr, 0, middle);
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
int mid = (right + left) / 2;

return merge(sort(left), sort(right));
//分
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
//治
merge(arr, left, mid, right);
}

protected int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int i = 0;
while (left.length > 0 && right.length > 0) {
if (left[0] <= right[0]) {
result[i++] = left[0];
left = Arrays.copyOfRange(left, 1, left.length);

/**
* 主要思想, 两个已经排序好的数组, 如何归并
* 当元素个数趋近单个元素时, 归并即为排序过程
*/
private void merge(int[] arr, int left, int mid, int right) {
int[] tmp = new int[right - left + 1];

int i = left;
int j = mid + 1;
int k = 0;

while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) { //取等号, 优先匹配左边数组予以填充
tmp[k++] = arr[i++];
} else {
result[i++] = right[0];
right = Arrays.copyOfRange(right, 1, right.length);
tmp[k++] = arr[j++];
}
}

while (left.length > 0) {
result[i++] = left[0];
left = Arrays.copyOfRange(left, 1, left.length);
}

while (right.length > 0) {
result[i++] = right[0];
right = Arrays.copyOfRange(right, 1, right.length);
}
//队尾的直接填充
while (i <= mid) tmp[k++] = arr[i++];
while (j <= right) tmp[k++] = arr[j++];

return result;
//覆盖原数组
System.arraycopy(tmp, 0, arr, left, tmp.length);
}

}
```

Expand Down
5 changes: 1 addition & 4 deletions src/java/main/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
public class BubbleSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
for (int i = 1; i < arr.length; i++) {
// 设定一个标记,若为true,则表示此次循环没有进行交换,也就是待排序列已经有序,排序已经完成。
boolean flag = true;
Expand Down
5 changes: 1 addition & 4 deletions src/java/main/BucketSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class BucketSort implements IArraySort {
private static final InsertSort insertSort = new InsertSort();

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
return bucketSort(arr, 5);
}

Expand Down
5 changes: 1 addition & 4 deletions src/java/main/CountingSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
public class CountingSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
int maxValue = getMaxValue(arr);

return countingSort(arr, maxValue);
Expand Down
5 changes: 1 addition & 4 deletions src/java/main/HeapSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
public class HeapSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
int len = arr.length;

buildMaxHeap(arr, len);
Expand Down
5 changes: 1 addition & 4 deletions src/java/main/InsertSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
public class InsertSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
// 从下标为1的元素开始选择合适的位置插入,因为下标为0的只有一个元素,默认是有序的
for (int i = 1; i < arr.length; i++) {

Expand Down
69 changes: 37 additions & 32 deletions src/java/main/MergeSort.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import java.util.Arrays;

/**
* 归并排序
* @author qrqhuangcy
* @date 2020/5/4
*/
public class MergeSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
public int[] sort(int[] arr) throws Exception {
mergeSort(arr, 0, arr.length - 1);
return arr;
}

if (arr.length < 2) {
return arr;
private void mergeSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int middle = (int) Math.floor(arr.length / 2);

int[] left = Arrays.copyOfRange(arr, 0, middle);
int[] right = Arrays.copyOfRange(arr, middle, arr.length);
int mid = (right + left) / 2;

return merge(sort(left), sort(right));
//分
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
//治
merge(arr, left, mid, right);
}

protected int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int l = 0, r = 0, len = 0;
while (len < left.length + right.length) {
if (left[l] <= right[r]) {
result[len++] = left[l++];

if (l == left.length) {
for (int i = r; i < right.length; i++) {
result[len++] = right[r++];
}
}
} else {
result[len++] = right[r++];

if (r == right.length) {
for (int i = l; i < left.length; i++) {
result[len++] = left[l++];
}
}
/**
* 主要思想, 两个已经排序好的数组, 如何归并
* 当元素个数趋近单个元素时, 归并即为排序过程
*/
private void merge(int[] arr, int left, int mid, int right) {
int[] tmp = new int[right - left + 1];

int i = left;
int j = mid + 1;
int k = 0;

while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) { //取等号, 优先匹配左边数组予以填充
tmp[k++] = arr[i++];
} else {
tmp[k++] = arr[j++];
}
}

return result;
}
//队尾的直接填充
while (i <= mid) tmp[k++] = arr[i++];
while (j <= right) tmp[k++] = arr[j++];

//覆盖原数组
System.arraycopy(tmp, 0, arr, left, tmp.length);
}
}
5 changes: 1 addition & 4 deletions src/java/main/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
public class QuickSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
return quickSort(arr, 0, arr.length - 1);
}

Expand Down
5 changes: 1 addition & 4 deletions src/java/main/RadixSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
public class RadixSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
int maxDigit = getMaxDigit(arr);
return radixSort(arr, maxDigit);
}
Expand Down
4 changes: 1 addition & 3 deletions src/java/main/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
public class SelectionSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
// 总共要经过 N-1 轮比较
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
Expand Down
7 changes: 1 addition & 6 deletions src/java/main/ShellSort.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import java.util.Arrays;

/**
* 希尔排序
*/
public class ShellSort implements IArraySort {

@Override
public int[] sort(int[] sourceArray) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

public int[] sort(int[] arr) throws Exception {
int gap = 1;
while (gap < arr.length) {
gap = gap * 3 + 1;
Expand Down
3 changes: 3 additions & 0 deletions src/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
</dependencies>

<build>
<sourceDirectory>main</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
5 changes: 0 additions & 5 deletions src/java/target/maven-archiver/pom.properties

This file was deleted.

Binary file removed src/java/target/sort-1.0-SNAPSHOT.jar
Binary file not shown.
7 changes: 0 additions & 7 deletions src/java/test/ArraySortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ public void mergeSort() throws Exception {
assertArrayEquals(sortedArray, new MergeSort().sort(array));
}

@Test
public void mergeSort_merge() throws Exception {
assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1, 2}, new int[]{}));
assertArrayEquals(new int[]{1, 2}, new MergeSort().merge(new int[]{1}, new int[]{2}));
assertArrayEquals(new int[]{1, 2, 3}, new MergeSort().merge(new int[]{1, 3}, new int[]{2}));
}

@Test
public void quickSort() throws Exception {
assertArrayEquals(sortedArray, new QuickSort().sort(array));
Expand Down