diff --git a/src/main/java/com/lin/App.java b/src/main/java/com/lin/App.java index 1133952050327f6237c770ee4b996fac7e8f0222..0b491be7cf3e609fa4a1613120264013456f1190 100644 --- a/src/main/java/com/lin/App.java +++ b/src/main/java/com/lin/App.java @@ -9,13 +9,6 @@ import java.util.Map; public class App { public static void main(String[] args) { - System.out.println("Hello Utils"); - Map m = new HashMap<>(); - m.put("ss", "ghjk"); - m.put("ss", 23456); - - Object s = m.get("s"); - } } diff --git a/src/main/java/com/lin/enums/StatusCode.java b/src/main/java/com/lin/enums/StatusCode.java new file mode 100644 index 0000000000000000000000000000000000000000..dadd128b86274d9faaf17b1ea716a04422562120 --- /dev/null +++ b/src/main/java/com/lin/enums/StatusCode.java @@ -0,0 +1,34 @@ +package com.lin.enums; + +public enum StatusCode { + + + MIX_SORT(-1, "小到大"), + MAX_SORT(1, "大到小"), + EQUAL(0, "相等"); + + + int code; + String desc; + + StatusCode(int code, String desc) { + this.code = code; + this.desc = desc; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/src/main/java/com/lin/sort/BubbleSort.java b/src/main/java/com/lin/sort/BubbleSort.java new file mode 100644 index 0000000000000000000000000000000000000000..2bbfa36e6f0d666eb880e0aecdd32c843c1e7f2f --- /dev/null +++ b/src/main/java/com/lin/sort/BubbleSort.java @@ -0,0 +1,61 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 冒泡排序 + *

O(n)~O(n^2)~O(n^2)

+ * 稳定 + */ +public class BubbleSort { + + + /** + * 冒泡排序 + * + * @param array + * @param statusCode + */ + public static void bubbleSorted(int[] array, StatusCode statusCode) { + for (int i = 0; i < array.length; i++) { + for (int j = 0; j < array.length; j++) { + int falg = SortUtil.compare(array[i], array[j]); + //可修改排序方向 + if (falg == statusCode.getCode()) { + SortUtil.swap(array, i, j); + } + } + } + } + + + /** + * 鸡尾酒排序 + * + * @param array + * @param n + */ + public static void CocktailSort(int[] array, int n) { + int left = 0; // 初始化边界 + int right = n - 1; + while (left < right) { + for (int i = left; i < right; i++) // 前半轮,将最大元素放到后面 + { + if (array[i] > array[i + 1]) { + SortUtil.swap(array, i, i + 1); + } + } + right--; + for (int i = right; i > left; i--) // 后半轮,将最小元素放到前面 + { + if (array[i - 1] > array[i]) { + SortUtil.swap(array, i - 1, i); + } + } + left++; + } + } + + +} diff --git a/src/main/java/com/lin/sort/BucketSort.java b/src/main/java/com/lin/sort/BucketSort.java new file mode 100644 index 0000000000000000000000000000000000000000..9dab05fe57dbc920f34946ba014ba0bb56183bcc --- /dev/null +++ b/src/main/java/com/lin/sort/BucketSort.java @@ -0,0 +1,86 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; + +import java.util.Arrays; + +public class BucketSort { + + + /** + * 桶排序 + * + * @param sourceArray + * @return + * @throws Exception + */ + public static int[] bucketSorted(int[] sourceArray) { + // 对 arr 进行拷贝,不改变参数内容 + int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); + + return bucketSort(arr, 5); + } + + /** + * 桶排序 + * + * @param arr + * @param bucketSize + * @return + * @throws Exception + */ + private static int[] bucketSort(int[] arr, int bucketSize) { + if (arr.length == 0) { + return arr; + } + + int minValue = arr[0]; + int maxValue = arr[0]; + for (int value : arr) { + if (value < minValue) { + minValue = value; + } else if (value > maxValue) { + maxValue = value; + } + } + + int bucketCount = (int) Math.floor((maxValue - minValue) / bucketSize) + 1; + int[][] buckets = new int[bucketCount][0]; + + // 利用映射函数将数据分配到各个桶中 + for (int i = 0; i < arr.length; i++) { + int index = (int) Math.floor((arr[i] - minValue) / bucketSize); + buckets[index] = arrAppend(buckets[index], arr[i]); + } + + int arrIndex = 0; + for (int[] bucket : buckets) { + if (bucket.length <= 0) { + continue; + } + // 对每个桶进行排序,这里使用了插入排序 + + InsertionSort.insertionSorted(bucket, StatusCode.MIX_SORT); + +// bucket = insertSort.sort(bucket); + for (int value : bucket) { + arr[arrIndex++] = value; + } + } + + return arr; + } + + /** + * 自动扩容,并保存数据 + * + * @param arr + * @param value + */ + private static int[] arrAppend(int[] arr, int value) { + arr = Arrays.copyOf(arr, arr.length + 1); + arr[arr.length - 1] = value; + return arr; + } + +} diff --git a/src/main/java/com/lin/sort/CountingSort.java b/src/main/java/com/lin/sort/CountingSort.java new file mode 100644 index 0000000000000000000000000000000000000000..f000a3a4f4f9d8031f78879a583809c43b63fa9d --- /dev/null +++ b/src/main/java/com/lin/sort/CountingSort.java @@ -0,0 +1,66 @@ +package com.lin.sort; + +import java.util.Arrays; + +/** + * 计数排序 + */ +public class CountingSort { + + + /** + * 计数排序 + * + * @param sourceArray + * @return 返回排序后的数组 + */ + public static int[] countingsorted(int[] sourceArray) { + // 对 arr 进行拷贝,不改变参数内容 + int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); + + int maxValue = getMaxValue(arr); + + return sort(arr, maxValue); + } + + /** + * 统计排序 + * + * @param arr + * @param maxValue + * @return + */ + private static int[] sort(int[] arr, int maxValue) { + int bucketLen = maxValue + 1; + int[] bucket = new int[bucketLen]; + + for (int value : arr) { + bucket[value]++; + } + + int sortedIndex = 0; + for (int j = 0; j < bucketLen; j++) { + while (bucket[j] > 0) { + arr[sortedIndex++] = j; + bucket[j]--; + } + } + return arr; + } + + /** + * 获取最大值 + * + * @param arr + * @return + */ + private static int getMaxValue(int[] arr) { + int maxValue = arr[0]; + for (int value : arr) { + if (maxValue < value) { + maxValue = value; + } + } + return maxValue; + } +} diff --git a/src/main/java/com/lin/sort/HeapSort.java b/src/main/java/com/lin/sort/HeapSort.java new file mode 100644 index 0000000000000000000000000000000000000000..6ab5f7f907cd86e490642c715394e94b697eb9e7 --- /dev/null +++ b/src/main/java/com/lin/sort/HeapSort.java @@ -0,0 +1,70 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 堆排序 + *

O(nlogn)~O(nlogn)~O(nlogn)

+ * 不稳定 + */ +public class HeapSort { + + /** + * 堆排序 + * + * @param array + * @param statusCode + */ + public static void heapSorted(int[] array, StatusCode statusCode) { + int heap_size = buildHeap(array, array.length, statusCode); // 建立一个最大堆 + while (heap_size > 1) { // 堆(无序区)元素个数大于1,未完成排序 + // 将堆顶元素与堆的最后一个元素互换,并从堆中去掉最后一个元素 + // 此处交换操作很有可能把后面元素的稳定性打乱,所以堆排序是不稳定的排序算法 + SortUtil.swap(array, 0, --heap_size); + heapify(array, 0, heap_size, statusCode); // 从新的堆顶元素开始向下进行堆调整,时间复杂度O(logn) + } + } + + + /** + * 堆结构调整 + * + * @param array + * @param i + * @param size + */ + private static void heapify(int[] array, int i, int size, StatusCode statusCode) // 从A[i]向下进行堆调整 + { + int left_child = 2 * i + 1; // 左孩子索引 + int right_child = 2 * i + 2; // 右孩子索引 + int max = i; // 选出当前结点与其左右孩子三者之中的最大值 + if (left_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[left_child])) +// if (left_child < size && array[left_child] > array[max]) + max = left_child; + if (right_child < size && statusCode.getCode() == SortUtil.compare(array[max], array[right_child])) +// if (right_child < size && array[right_child] > array[max]) + max = right_child; + if (max != i) { + SortUtil.swap(array, i, max); // 把当前结点和它的最大(直接)子节点进行交换 + heapify(array, max, size, statusCode); // 递归调用,继续从当前结点向下进行堆调整 + } + } + + /** + * 构造堆 + * + * @param array + * @param n + * @return + */ + private static int buildHeap(int[] array, int n, StatusCode statusCode) // 建堆,时间复杂度O(n) + { + int heap_size = n; + for (int i = heap_size / 2 - 1; i >= 0; i--) // 从每一个非叶结点开始向下进行堆调整 + heapify(array, i, heap_size, statusCode); + return heap_size; + } + + +} diff --git a/src/main/java/com/lin/sort/InsertionSort.java b/src/main/java/com/lin/sort/InsertionSort.java new file mode 100644 index 0000000000000000000000000000000000000000..f19c696bf14a8d15a0fc80c1b4d34d19fe5ff3ed --- /dev/null +++ b/src/main/java/com/lin/sort/InsertionSort.java @@ -0,0 +1,66 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 插入排序 + *

O(n)~O(n^2)~O(n^2)

+ * 稳定 + */ +public class InsertionSort { + + + /** + * 插入排序 + * + * @param array + * @param statusCode + */ + public static void insertionSorted(int[] array, StatusCode statusCode) { + //默认第0个是排好序的,i以前的排好序 + for (int i = 1; i < array.length; i++) { + int get = array[i]; + int j = i - 1; + //从当前位值倒序往前查找 + while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) { + array[j + 1] = array[j]; + j--; + } + //不满足比较条件时放在位置的右边 + array[j + 1] = get; + } + + } + + /** + * 二分插入排序 + * + * @param array + * @param statusCode + */ + public static void insertionSortedDichotomy(int[] array, StatusCode statusCode) { + for (int i = 1; i < array.length; i++) { + int get = array[i]; + int left = 0; + int right = i - 1; + //二分查找数据所处的位置,也是在之前的队列中 + while (left <= right) + { + int mid = (left + right) / 2; + if (statusCode.getCode() == SortUtil.compare(get,array[mid])) +// if (array[mid] > get) + right = mid - 1; + else + left = mid + 1; + } + //拿走的是位置i的数据,left之后的往后移动 + for (int j = i - 1; j >= left; j--) + { + array[j + 1] = array[j]; + } + array[left] = get; + } + } + +} diff --git a/src/main/java/com/lin/sort/MergeSort.java b/src/main/java/com/lin/sort/MergeSort.java new file mode 100644 index 0000000000000000000000000000000000000000..5df23b9f949973600d3a134daa4693c69ff8d39e --- /dev/null +++ b/src/main/java/com/lin/sort/MergeSort.java @@ -0,0 +1,89 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 归并排序 + *

O(nlogn)~O(nlogn)~O(nlogn)

+ * 稳定 + */ +public class MergeSort { + + + /** + * 非递归归并排序(错误) + * + * @param array + * @param statusCode + */ + public static void mergeSortedIteration(int array[], StatusCode statusCode) { + int left, mid, right; + for (int i = 1; i < array.length; i *= 2) { + left = 0; + while (left + i < array.length) { + mid = left + i - 1; + right = mid + 1 < array.length ? mid + 1 : array.length - 1; + merge(array, statusCode, left, mid, right); + left = mid + 1; + } + } + } + + + /** + * 递归使用归并排序 + * + * @param array + * @param statusCode + * @param left + * @param right + */ + public static void mergeSortedRecursion(int[] array, StatusCode statusCode, int left, int right) { + if (left == right) + return; + int mid = (left + right) / 2; + //前半段数组归并 + mergeSortedRecursion(array, statusCode, left, mid); + //后半段数组归并 + mergeSortedRecursion(array, statusCode, mid + 1, right); + //归并合并 + merge(array, statusCode, left, mid, right); + } + + /** + * 数组合并 + * + * @param array + * @param statusCode + * @param left + * @param mid + * @param right + */ + private static void merge(int[] array, StatusCode statusCode, int left, int mid, int right) { + int len = right - left + 1; + int[] temp = new int[len]; + int index = 0; + //前半段数组起始 + int i = left; + //后半段数组起始 + int j = mid + 1; + //两组数据归并操作 + while (i <= mid && j <= right) { + temp[index++] = statusCode.getCode() == SortUtil.compare(array[i], array[j]) ? array[i++] : array[j++]; + } + //上面的循环无法将两个子数组的数据全部循环到 + while (i <= mid) { + temp[index++] = array[i++]; + } + while (j <= right) { + temp[index++] = array[j++]; + } + //数据放入原 + int tindex = 0; + while (tindex < temp.length) { + array[left++] = temp[tindex++]; + } + + } +} diff --git a/src/main/java/com/lin/sort/QuickSort.java b/src/main/java/com/lin/sort/QuickSort.java new file mode 100644 index 0000000000000000000000000000000000000000..f4f76c4e3f82b72927e0890ef89c9d6d09fa599d --- /dev/null +++ b/src/main/java/com/lin/sort/QuickSort.java @@ -0,0 +1,52 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 快速排序 + *

O(nlogn)~O(nlogn)~O(n^2)

+ * 不稳定 + */ +public class QuickSort { + + + /** + * 分治块 + * + * @param array + * @param left + * @param right + * @param statusCode + * @return + */ + private static int partition(int[] array, int left, int right, StatusCode statusCode) // 划分函数 + { + int pivot = array[right]; // 这里每次都选择最后一个元素作为基准 + int tail = left - 1; // tail为小于基准的子数组最后一个元素的索引 + for (int i = left; i < right; i++) // 遍历基准以外的其他元素 + { + if (SortUtil.compare(pivot, array[i]) == 0 || !(statusCode.getCode() == SortUtil.compare(pivot, array[i]))) //会破环稳定性 +// if (array[i] <= pivot) // 把小于等于基准的元素放到前一个子数组末尾 + { + SortUtil.swap(array, ++tail, i); + } + } + SortUtil.swap(array, tail + 1, right); // 最后把基准放到前一个子数组的后边,剩下的子数组既是大于基准的子数组 + // 该操作很有可能把后面元素的稳定性打乱,所以快速排序是不稳定的排序算法 + return tail + 1; // 返回基准的索引 + } + + /** + * 快速排序 + */ + public static void quickSorted(int[] array, int left, int right, StatusCode statusCode) { + if (left >= right) + return; + int pivot_index = partition(array, left, right, statusCode); // 基准的索引 + quickSorted(array, left, pivot_index - 1, statusCode); + quickSorted(array, pivot_index + 1, right, statusCode); + } + + +} diff --git a/src/main/java/com/lin/sort/RadixSort.java b/src/main/java/com/lin/sort/RadixSort.java new file mode 100644 index 0000000000000000000000000000000000000000..6c66253f38846e2914347e58d43f9949e2812eca --- /dev/null +++ b/src/main/java/com/lin/sort/RadixSort.java @@ -0,0 +1,107 @@ +package com.lin.sort; + +import java.util.Arrays; + +/** + * 基数排序 + */ +public class RadixSort { + + + /** + * 基数排序 + * + * @param sourceArray + * @return + */ + public static int[] radixSorted(int[] sourceArray) { + // 对 arr 进行拷贝,不改变参数内容 + int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); + + int maxDigit = getMaxDigit(arr); + return radixSort(arr, maxDigit); + } + + /** + * 获取最高位数 + * + * @param arr + * @return + */ + private static int getMaxDigit(int[] arr) { + int maxValue = getMaxValue(arr); + return getNumLenght(maxValue); + } + + /** + * 获取最大值 + * + * @param arr + * @return + */ + private static int getMaxValue(int[] arr) { + int maxValue = arr[0]; + for (int value : arr) { + if (maxValue < value) { + maxValue = value; + } + } + return maxValue; + } + + protected static int getNumLenght(long num) { + if (num == 0) { + return 1; + } + int lenght = 0; + for (long temp = num; temp != 0; temp /= 10) { + lenght++; + } + return lenght; + } + + /** + * 基数排序 + * + * @param arr + * @param maxDigit + * @return + */ + private static int[] radixSort(int[] arr, int maxDigit) { + int mod = 10; + int dev = 1; + + for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) { + // 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10) + int[][] counter = new int[mod * 2][0]; + + for (int j = 0; j < arr.length; j++) { + int bucket = ((arr[j] % mod) / dev) + mod; + counter[bucket] = arrayAppend(counter[bucket], arr[j]); + } + + int pos = 0; + for (int[] bucket : counter) { + for (int value : bucket) { + arr[pos++] = value; + } + } + } + + return arr; + } + + /** + * 自动扩容,并保存数据 + * + * @param arr + * @param value + */ + private static int[] arrayAppend(int[] arr, int value) { + arr = Arrays.copyOf(arr, arr.length + 1); + arr[arr.length - 1] = value; + return arr; + } + + +} diff --git a/src/main/java/com/lin/sort/SelectionSort.java b/src/main/java/com/lin/sort/SelectionSort.java new file mode 100644 index 0000000000000000000000000000000000000000..3b6e7d3db5ac2c5d49bb47a5122dc585c63b5879 --- /dev/null +++ b/src/main/java/com/lin/sort/SelectionSort.java @@ -0,0 +1,42 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 选择排序 + *

O(n^2)~O(n^2)~O(n^2)

+ * 不稳定 + */ +public class SelectionSort { + + + /** + * 选择排序 + * + * @param array + * @param statusCode + */ + public static void selectionSorted(int[] array, StatusCode statusCode) { + //确定最小的位置 + for (int i = 0; i < array.length; i++) { + int min = i; + //确定最小的位置存放的数据 + for (int j = i + 1; j < array.length; j++) { + //用标志为查出为排序队列中最小/大的 + if (statusCode.getCode() == SortUtil.compare(array[j], array[min])) { + min = j; + } + } + //将最小的放在最前面 + if (min != i) { + SortUtil.swap(array, i, min); + } + + } + + + } + + +} diff --git a/src/main/java/com/lin/sort/ShellSort.java b/src/main/java/com/lin/sort/ShellSort.java new file mode 100644 index 0000000000000000000000000000000000000000..9bc773920f250d02b42ea2a516efc51a1bd078f7 --- /dev/null +++ b/src/main/java/com/lin/sort/ShellSort.java @@ -0,0 +1,44 @@ +package com.lin.sort; + +import com.lin.enums.StatusCode; +import com.lin.sort.util.SortUtil; + +/** + * 希尔排序 + *

O(n^1.3)~O(nlogn)~O(n^2)~O(n^2)

+ * 不稳定 + */ +public class ShellSort { + + + /** + * 希尔排序 + * + * @param array + * @param statusCode + */ + public static void shellSorted(int[] array, StatusCode statusCode) { + int shell = 0; + //寻找最大的希尔值 + while (shell <= array.length) { + shell = shell * 3 + 1; + } + //通过希尔来降低排序复杂度 + while (shell >= 1) { + //确定希尔值后,内部使用呢直接插入排序 + for (int i = shell; i < array.length; i++) { + int j = i - shell; + int get = array[i]; + while (j >= 0 && statusCode.getCode() == SortUtil.compare(get, array[j])) { + array[j + shell] = array[j]; + j = j - shell; + } + array[j + shell] = get; + } + shell = (shell - 1) / 3; + } + + + } + +} diff --git a/src/main/java/com/lin/sort/util/SortUtil.java b/src/main/java/com/lin/sort/util/SortUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..331e81f7af337e46802c2723ffef5e6fe3abb77b --- /dev/null +++ b/src/main/java/com/lin/sort/util/SortUtil.java @@ -0,0 +1,38 @@ +package com.lin.sort.util; + +/** + * 排序工具 + */ +public class SortUtil { + + /** + * 交换 + * + * @param array + * @param a + * @param b + */ + public static void swap(int[] array, int a, int b) { + int temp = array[a]; + array[a] = array[b]; + array[b] = temp; + } + + /** + * 比较器 + * + * @param a + * @param b + * @return + */ + public static int compare(int a, int b) { + if (a > b) { + return 1; + } else if (a < b) { + return -1; + } else { + return 0; + } + } + +} diff --git a/src/test/java/com/lin/TestSort/BubbleSortTest.java b/src/test/java/com/lin/TestSort/BubbleSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ba6176f0da3f7449d5531ba76747c6355da17226 --- /dev/null +++ b/src/test/java/com/lin/TestSort/BubbleSortTest.java @@ -0,0 +1,20 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.BubbleSort; +import org.junit.Test; + +public class BubbleSortTest { + + + @Test + public void bubbleSortedTest(){ + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + BubbleSort.bubbleSorted(a, StatusCode.MAX_SORT); + for (int i : a) { + System.out.println(i); + } + + } + +} diff --git a/src/test/java/com/lin/TestSort/BucketSortTest.java b/src/test/java/com/lin/TestSort/BucketSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..41abc9e1425964ea5f1c7b99d75c2fd257209d73 --- /dev/null +++ b/src/test/java/com/lin/TestSort/BucketSortTest.java @@ -0,0 +1,17 @@ +package com.lin.TestSort; + +import com.lin.sort.BucketSort; +import org.junit.Test; + +public class BucketSortTest { + + + @Test + public void bucketSortedTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + int[] countingsorted = BucketSort.bucketSorted(a); + for (int i : countingsorted) { + System.out.println(i); + } + } +} diff --git a/src/test/java/com/lin/TestSort/CountingSortTest.java b/src/test/java/com/lin/TestSort/CountingSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ee183b38b2895c3e74841f8993e3fba3a7ae5a3d --- /dev/null +++ b/src/test/java/com/lin/TestSort/CountingSortTest.java @@ -0,0 +1,17 @@ +package com.lin.TestSort; + +import com.lin.sort.CountingSort; +import org.junit.Test; + +public class CountingSortTest { + + + @Test + public void countingSortedTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + int[] countingsorted = CountingSort.countingsorted(a); + for (int i : countingsorted) { + System.out.println(i); + } + } +} diff --git a/src/test/java/com/lin/TestSort/HeapSortTest.java b/src/test/java/com/lin/TestSort/HeapSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..fe9a2fed853ad1b3cb41cc4c1ebdab195a05a9d6 --- /dev/null +++ b/src/test/java/com/lin/TestSort/HeapSortTest.java @@ -0,0 +1,19 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.HeapSort; +import org.junit.Test; + +public class HeapSortTest { + + + @Test + public void heapSrotTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + HeapSort.heapSorted(a, StatusCode.MAX_SORT); + for (int i : a) { + System.out.println(i); + } + } + +} diff --git a/src/test/java/com/lin/TestSort/InsertionSortTest.java b/src/test/java/com/lin/TestSort/InsertionSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..267f3245993ae7a38f1c54903b2818540bb2aaac --- /dev/null +++ b/src/test/java/com/lin/TestSort/InsertionSortTest.java @@ -0,0 +1,29 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.InsertionSort; +import org.junit.Test; + +public class InsertionSortTest { + + + @Test + public void insertionSortedtest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + InsertionSort.insertionSorted(a, StatusCode.MIX_SORT); + for (int i : a) { + System.out.println(i); + } + } + + @Test + public void insertionSortedDichotomyTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + InsertionSort.insertionSortedDichotomy(a, StatusCode.MIX_SORT); + for (int i : a) { + System.out.println(i); + } + } + + +} diff --git a/src/test/java/com/lin/TestSort/MergeSortTest.java b/src/test/java/com/lin/TestSort/MergeSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8e8897788a3ab91cf6ffc74e0a7842e49a35d4ed --- /dev/null +++ b/src/test/java/com/lin/TestSort/MergeSortTest.java @@ -0,0 +1,28 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.MergeSort; +import org.junit.Test; + +public class MergeSortTest { + + + @Test + public void mergeSortedRecursionTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + MergeSort.mergeSortedRecursion(a, StatusCode.MAX_SORT, 0, a.length - 1); + for (int i : a) { + System.out.println(i); + } + } + + @Test + public void mergeSortedIterationTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + MergeSort.mergeSortedIteration(a, StatusCode.MIX_SORT); + for (int i : a) { + System.out.println(i); + } + } + +} diff --git a/src/test/java/com/lin/TestSort/QuickSortedTest.java b/src/test/java/com/lin/TestSort/QuickSortedTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2127f637043cef909152f7639ceb5e9e89a338de --- /dev/null +++ b/src/test/java/com/lin/TestSort/QuickSortedTest.java @@ -0,0 +1,19 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.QuickSort; +import org.junit.Test; + +public class QuickSortedTest { + + + @Test + public void quickSortedTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + QuickSort.quickSorted(a, 0, a.length - 1, StatusCode.MAX_SORT); + for (int i : a) { + System.out.println(i); + } + } + +} diff --git a/src/test/java/com/lin/TestSort/RadixSortTest.java b/src/test/java/com/lin/TestSort/RadixSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bc424bdbca609481f485f23e6714b55223daf917 --- /dev/null +++ b/src/test/java/com/lin/TestSort/RadixSortTest.java @@ -0,0 +1,20 @@ +package com.lin.TestSort; + +import com.lin.sort.RadixSort; +import org.junit.Test; + +public class RadixSortTest { + + + @Test + public void radixSortedTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + int[] countingsorted = RadixSort.radixSorted(a); + for (int i : countingsorted) { + System.out.println(i); + } + } + + + +} diff --git a/src/test/java/com/lin/TestSort/SelectionSortTest.java b/src/test/java/com/lin/TestSort/SelectionSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3981aa6331e3cab68f4b2a472aa5f418eb44ed3c --- /dev/null +++ b/src/test/java/com/lin/TestSort/SelectionSortTest.java @@ -0,0 +1,18 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.SelectionSort; +import org.junit.Test; + +public class SelectionSortTest { + + + @Test + public void selectionSortedTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + SelectionSort.selectionSorted(a, StatusCode.MIX_SORT); + for (int i : a) { + System.out.println(i); + } + } +} diff --git a/src/test/java/com/lin/TestSort/ShellSortTest.java b/src/test/java/com/lin/TestSort/ShellSortTest.java new file mode 100644 index 0000000000000000000000000000000000000000..86807ae0666ac9e8760264300b0cd6e42b4f6b17 --- /dev/null +++ b/src/test/java/com/lin/TestSort/ShellSortTest.java @@ -0,0 +1,19 @@ +package com.lin.TestSort; + +import com.lin.enums.StatusCode; +import com.lin.sort.ShellSort; +import org.junit.Test; + +public class ShellSortTest { + + @Test + public void shellSortTest() { + int[] a = {1, 6, 8, 2, 4, 5, 7, 1, 0}; + ShellSort.shellSorted(a, StatusCode.MIX_SORT); + for (int i : a) { + System.out.println(i); + } + } + + +} diff --git a/src/test/java/com/lin/TestSort/v3.0.md b/src/test/java/com/lin/TestSort/v3.0.md new file mode 100644 index 0000000000000000000000000000000000000000..4305f784f548b7a44cb2735377ad06610c5087c2 --- /dev/null +++ b/src/test/java/com/lin/TestSort/v3.0.md @@ -0,0 +1,9 @@ +# V3.0 + +1. 冒泡排序工具 +2. 选择排序 +3. 插入排序 +4. 选择排序 +5. 归并排序 +6. 希尔排序 +7. 堆排序 \ No newline at end of file