package com;
/**
* 实现插入排序
* @author 小小王
*
*/
public class TestInsertSort {
public static void insertSort(int[] a){
for(int i = 1; i < a.length; i++){
int temp = a[i];
int j = i;
if(a[j-1]>temp){
while(j >= 1 && a[j-1] > temp){
a[j] = a[j-1];
j --;
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = {4,6,2,4,8,5,2,9};
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
/**
* 实现插入排序
* @author 小小王
*
*/
public class TestInsertSort {
public static void insertSort(int[] a){
for(int i = 1; i < a.length; i++){
int temp = a[i];
int j = i;
if(a[j-1]>temp){
while(j >= 1 && a[j-1] > temp){
a[j] = a[j-1];
j --;
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = {4,6,2,4,8,5,2,9};
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}