Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
DMinerJackie committed Jan 13, 2019
1 parent 6988aab commit 4cbaebb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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] + " ");
}
}
}

0 comments on commit 4cbaebb

Please sign in to comment.