算法 | 是否为原地排序 | 时间复杂度 | 空间复杂度 |
---|---|---|---|
插入排序 | 是 | 介于N和N2之间 | 1 |
public class InsertionSort {
public static void main(String[] args)
{
int[] array = {5,4,3,2,1};
for (int i = 1;i < array.length;i++)
{
for (int j = i;j > 0 && array[j-1] > array[j];j--)
{
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
//out:1 2 3 4 5
for (int i = 0;i < array.length;i++)
System.out.print(array[i] + " ");
}
}