插入排序和运用的场景 java语言

选择排序:稳定

适用于:数据量不大,并且对稳定性有要求并且数据局部或者整体有序的情况。

public class InserSort {
public static void main(String[] args) {
int[] a= {1,4,3,2,6,5};
insertSort(a);
System.out.println(Arrays.toString(a));

}
public static void insertSort(int[] a) {
for(int i=1;i<a.length;i++) {
int j=i;
int temp=a[i];
if(a[j-1]>temp) {
while(j>=1&&a[j-1]>temp) {
a[j]=a[j-1];
j--;
}
}
a[j]=temp;

}
}
}

猜你喜欢

转载自www.cnblogs.com/sgbe/p/10760659.html