From 6cba69f2ca327fefd375078740a1b144c9cc9346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=B8=E7=9A=93=E6=A5=A0?= <1026609323@qq.com> Date: Fri, 8 Jan 2021 13:43:53 +0800 Subject: [PATCH] push --- work/Sort.java | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ work/Test.java | 23 +++++++++ 2 files changed, 147 insertions(+) create mode 100644 work/Sort.java create mode 100644 work/Test.java diff --git a/work/Sort.java b/work/Sort.java new file mode 100644 index 0000000..dde341f --- /dev/null +++ b/work/Sort.java @@ -0,0 +1,124 @@ +import java.sql.Time; +import java.time.temporal.Temporal; + +public class Sort { + + public void selectSort(int[] arr) { + int min; + //外圈决定最小数存放的位置,第一趟值存在0,以此类推 + for (int i = 0; i < arr.length - 1; i++) { + min = i; + //内圈循环从原本的位置后一个开始往后遍历,找出最小值的index + for (int j = i + 1; j < arr.length; j++) { + if (arr[min] > arr[j]) { + min = j; + } + } + int temp = arr[i]; + arr[i] = arr[min]; + arr[min] = temp; + } + sortPrint(arr, "选择排序"); + } + + //插入排序:本质就是把一组数据的第一个树当成有序列,从下标1开始把值插入有序列种 + public void insertSort(int arr[]) { + //外圈循环决定多少个数需要"插进去" + for (int i = 1; i < arr.length; i++) { + //内圈循环把需要插入有序序列的值与 有序序列的最后一个值依次往前循环比较找到位置 + for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) { + int temp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = temp; + } + } + sortPrint(arr, "插入排序"); + } + + //冒泡排序:本质就是左右两个数两个比较,大的往后放 + public void bubbleSort(int[] arr) { + //外层控制趟数 + for (int i = 0; i < arr.length; i++) { + //内层控制所有数能两两比较一次 + for (int j = 0; j < arr.length - 1; j++) { + if (arr[j] > arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + sortPrint(arr, "冒泡排序"); + } + + //快速排序 + public void fastSort(int[] arr, int start, int end) { + if (end - start >= 1) { + int baseIndex = moveValue(arr, start, end); + fastSort(arr, start, baseIndex - 1); + fastSort(arr, baseIndex + 1, end); + } + } + + private int moveValue(int[] arr, int start, int end) { + //定义基数为第一个元素 + int baseIndex = start; + int baseValue = arr[start]; + while (start < end) { + //头部循环只要当前的索引的值小于基数,索引就继续往后走,知道找到大于基数的值的索引 + while (start < end && arr[start] <= arr[baseIndex]) { + start++; + } + //尾部循环找出小于基数的值的索引 + while (arr[end] > baseValue) { + end--; + } + //start>end证明整个数组没遍历完,将两边的值互换 + if (start < end) { + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + } + } + //退出循环说明end小于star,数组遍历完了,end的位置就是基数的位置,两个位置互换 + arr[baseIndex] = arr[end]; + arr[end] = baseValue; + baseIndex = end; + return baseIndex; + } + + //希尔排序本质就是插入排序加上增量 + public void shellSort(int[] arr) { + //设置一个增量为长的一半 + int gap = arr.length / 2; + //循环把增量再分一半 直到1 + //例如长度为10的数组 需要分组3次插入排序,三个增量为:5、2、1 + for (; gap >= 1; gap /= 2) { + //对数组按照增量的大小进行插入排序 例如增量到最后是1 就等同于一般的插入排序 + for (int i = 0; i < arr.length / gap; i++) { + shellInsert(arr, gap); + } + } + sortPrint(arr, "希尔排序"); + } + + public void shellInsert(int arr[], int gap) { + //循环比较插入 + for (int i = 0; i < arr.length && i + gap < arr.length; i++) { + int temp; + if (arr[i] > arr[i + gap]) { + temp = arr[i]; + arr[i] = arr[i + gap]; + arr[i + gap] = temp; + } + } + } + + public void sortPrint(int[] arr, String str) { + System.out.print(str); + for (int num : arr) { + System.out.print(" " + num); + } + System.out.println(""); + } +} diff --git a/work/Test.java b/work/Test.java new file mode 100644 index 0000000..19822c6 --- /dev/null +++ b/work/Test.java @@ -0,0 +1,23 @@ +public class Test { + public static void main(String[] args) { + int arr[] = new int[]{5, 2, 8, 36, 3, 89}; + Sort sort = new Sort(); + //选择排序 + sort.selectSort(arr); + //插入排序 + arr = new int[]{5, 2, 8, 36, 3, 89}; + sort.insertSort(arr); + //冒泡排序 + arr = new int[]{5, 2, 8, 36, 3, 89}; + sort.bubbleSort(arr); + //快速排序 + arr = new int[]{5, 2, 8, 36, 3, 89}; + sort.fastSort(arr, 0, arr.length - 1); + sort.sortPrint(arr, "快速排序"); + + //希尔排序 + arr = new int[]{5, 2, 8, 36, 3, 89}; + sort.shellSort(arr); + + } +} -- Gitee