diff --git a/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/BubbleSort.java b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/BubbleSort.java index e1e51b0..dcc2b34 100644 --- a/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/BubbleSort.java +++ b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/BubbleSort.java @@ -29,7 +29,7 @@ public static void main(String[] args) { */ public static void bubbleSort(int[] arr) { int length = arr.length; - if (length <= 0) { + if (length <= 1) { return; } @@ -78,7 +78,7 @@ public static void bubbleSort(int[] arr) { */ public static void bubbleSort2(int[] arr) { int length = arr.length; - if (length <= 0) { + if (length <= 1) { return; } diff --git a/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/InsertSort.java b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/InsertSort.java index ae5eb92..4a195dc 100644 --- a/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/InsertSort.java +++ b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/InsertSort.java @@ -36,7 +36,7 @@ public static void main(String[] args) { */ public static void insertSort(int arr[]) { int length = arr.length; - if (length <= 0) { + if (length <= 1) { return; } diff --git a/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/SelectionSort.java b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/SelectionSort.java new file mode 100644 index 0000000..63018eb --- /dev/null +++ b/algo/geek-time-algo/src/main/java/com/jackie/algo/geek/time/chapter11_sort/SelectionSort.java @@ -0,0 +1,37 @@ +package com.jackie.algo.geek.time.chapter11_sort; + +/** + * @Author: Jackie + * @date 2019/1/13 + */ +public class SelectionSort { + + public static void main(String[] args) { + int[] arr = new int[]{100,82,74,62,54,147}; + selectionSort(arr); + } + + public static void selectionSort(int[] arr) { + int length = arr.length; + if (length <= 1) return; + + for (int i = 0; i < length - 1; ++i) { + // 查找最小值 + int minIndex = i; + for (int j = i + 1; j < length; ++j) { + if (arr[j] < arr[minIndex]) { + minIndex = j; + } + } + + // 交换 + int tmp = arr[i]; + arr[i] = arr[minIndex]; + arr[minIndex] = tmp; + } + + for (int i = 0; i < length; i++) { + System.out.print(arr[i] + " "); + } + } +}