add insert sort

dev
ehlxr 2020-09-28 23:06:44 +08:00
parent 5aa8ebd5c8
commit eda1f371b5
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package me.ehlxr.leetcode;
import java.util.Arrays;
/**
*
*
*
*
* @author ehlxr
* @since 2020-09-28 22:30.
*/
public class InsertSort {
public static void sort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0; j--) {
int tmp = arr[j];
if (arr[j + 1] < arr[j]) {
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
} else {
break;
}
}
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
sort(new int[]{4, 9, 1, 6, 8, 2});
}
}