From e4d73624510e4cbb40adb2cadcbe62a1a1b4e480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87?= <975108392@qq.com> Date: Tue, 5 Jan 2021 22:40:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work/Sort.java | 182 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 work/Sort.java diff --git a/work/Sort.java b/work/Sort.java new file mode 100644 index 0000000..40b7ce5 --- /dev/null +++ b/work/Sort.java @@ -0,0 +1,182 @@ +public class Sort { + public static void main(String[] args) { + int[] arr = new int[10000000]; + for (int i = 0; i < 10000000; i++) { + arr[i] = (int) (Math.random() * 80000000); + } + + selectSort(arr); + int[] array = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8}; + int[] temp = new int[array.length]; + print(array); + MergeSort(array, 0, array.length - 1, temp); + print(array); + } + + //选择排序 + public static void selectSort(int[] array) { + for (int i = 0; i < array.length - 1; i++) { + for (int j = i + 1; j < array.length; j++) { + if (array[i] > array[j]) { + int temp = array[j]; + array[j] = array[i]; + array[i] = temp; + } + } + } + } + + //冒泡排序 + public static void bubbleSort(int[] array) { + int temp = 0; + for (int i = 0; i < array.length - 1; i++) { + for (int j = 0; j < array.length - 1 - i; j++) { + if (array[j] > array[j + 1]) { + temp = array[j + 1]; + array[j + 1] = array[j]; + array[j] = temp; + } + } + } + } + + //冒泡排序优化 + public static void bubbleSortBetter(int[] array) { + int temp = 0; + for (int i = 0; i < array.length - 1; i++) { + int exchageCount = 0; + for (int j = 0; j < array.length - 1 - i; j++) { + if (array[j] > array[j + 1]) { + exchageCount++; + temp = array[j + 1]; + array[j + 1] = array[j]; + array[j] = temp; + } + } + if (exchageCount == 0) { + break; + } + } + } + + + //插入排序 + public static void insertSort(int[] array) { + for (int i = 1; i < array.length; i++) { + int insertVal = array[i]; + int insertIndex = i - 1; + + + while (insertIndex >= 0 && insertVal < array[insertIndex]) { + array[insertIndex + 1] = array[insertIndex]; + insertIndex--; + } + + array[insertIndex + 1] = insertVal; + } + } + + + //希尔排序 + public static void shellSort(int[] array) { + int temp; + for (int grp = array.length / 2; grp > 0; grp /= 2) { + for (int i = grp; i < array.length; i++) { + for (int j = i - grp; j >= 0; j -= grp) { + if (array[j] > array[j + grp]) { + temp = array[j + grp]; + array[j + grp] = array[j]; + array[j] = temp; + } + } + } + } + } + + + //5: 快速排序 + public static void quickSort(int[] array, int leftIndex, int reightIndex) { + if (leftIndex >= reightIndex) { + return; + } + int left = leftIndex; + int reight = reightIndex; + int key = array[left]; + + while (left < reight) { + while (key <= array[reight] && left < reight) { + reight--; + } + array[left] = array[reight]; + while (key >= array[left] && left < reight) { + left++; + } + array[reight] = array[left]; + } + + array[left] = key; + quickSort(array, leftIndex, left - 1); + quickSort(array, reight + 1, reightIndex); + } + + + //归并排序 + public static void MergeSort(int[] array, int begin, int end, int[] temp) { + if (begin < end) { + int mid = (begin + end) / 2; + //递归 将序列不停的二分 + MergeSort(array, begin, mid, temp); + MergeSort(array, mid + 1, end, temp); + //合并 + merge(array, begin, mid, end, temp); + } + } + + public static void merge(int[] array, int begin, int mid, int end, int[] temp) { + + int i = begin; + int j = mid + 1; + int t = 0; + + while (i <= mid && j <= end) { + if (array[i] <= array[j]) { + temp[t] = array[i]; + t++; + i++; + } else { + temp[t] = array[j]; + t++; + j++; + } + } + + while (i <= mid) { + temp[t] = array[i]; + t++; + i++; + } + + while (j <= end) { + temp[t] = array[j]; + t++; + j++; + } + + t = 0; + //int tempLeft = begin; + while (begin <= end) { + array[begin] = temp[t]; + t++; + begin++; + } + + } + + + public static void print(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + " "); + } + System.out.println(); + } +} -- Gitee From bad679707a24c064de1a129d3769484853b94b03 Mon Sep 17 00:00:00 2001 From: wen <975108392@qq.com> Date: Thu, 7 Jan 2021 15:51:12 +0800 Subject: [PATCH 2/2] update work/Sort.java. --- work/Sort.java | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/work/Sort.java b/work/Sort.java index 40b7ce5..5a6c4d0 100644 --- a/work/Sort.java +++ b/work/Sort.java @@ -27,20 +27,6 @@ public class Sort { } //冒泡排序 - public static void bubbleSort(int[] array) { - int temp = 0; - for (int i = 0; i < array.length - 1; i++) { - for (int j = 0; j < array.length - 1 - i; j++) { - if (array[j] > array[j + 1]) { - temp = array[j + 1]; - array[j + 1] = array[j]; - array[j] = temp; - } - } - } - } - - //冒泡排序优化 public static void bubbleSortBetter(int[] array) { int temp = 0; for (int i = 0; i < array.length - 1; i++) { @@ -77,24 +63,8 @@ public class Sort { } - //希尔排序 - public static void shellSort(int[] array) { - int temp; - for (int grp = array.length / 2; grp > 0; grp /= 2) { - for (int i = grp; i < array.length; i++) { - for (int j = i - grp; j >= 0; j -= grp) { - if (array[j] > array[j + grp]) { - temp = array[j + grp]; - array[j + grp] = array[j]; - array[j] = temp; - } - } - } - } - } - - - //5: 快速排序 + + //快速排序 public static void quickSort(int[] array, int leftIndex, int reightIndex) { if (leftIndex >= reightIndex) { return; -- Gitee