package com.sf.test.test;
public class Sort {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i];
for (int j = i - 1; j >= 0; j--) {
if (arr[j] <= tmp) {
arr[j + 1] = tmp;
break;
}
arr[j + 1] = arr[j];
if (j == 0) {
arr[j] = tmp;
break;
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[] {5, 34, 3, 6, 454, 3324, 33};
insertionSort(arr);
for (int i : arr) {
System.out.println(i);
}
}
}
Insertion Sort
于 2021-01-20 12:10:42 首次发布