import java.util.Arrays;
public class InsertSort {
private static void insertSort(int[] arr)
{
int j;
int t;
for (int i = 1; i < arr.length; i++)
{
if (arr[i] < arr[i - 1])
{
t = arr[i];
for (j = i - 1; j >= 0 && arr[j] > t; j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = t;
}
}
}
public static void main(String[] args) {
int[] ints = {5, 3, 4, 1, 2};
insertSort(ints);
System.out.println(Arrays.toString(ints));
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-897.html