插入排序(插入位置,从头至尾搜索)

This commit is contained in:
chenrong 2018-11-29 17:05:38 +08:00
parent e9a0aa3dba
commit f74a14c60d

View File

@ -0,0 +1,48 @@
package sorts;
import java.util.Arrays;
/**
* 插入排序插入位置从头至尾搜索
* @Author ooooor
*/
public class InsertionSortAdd {
public static void main(String[] args) {
int[] data = new int[]{4, 6, 5, 3, 7, 1, 2};
fromStartToEnd(Arrays.copyOf(data, data.length));
System.out.println(Arrays.toString(data));
}
/**
* 查询插入位置时 从头至尾搜索
* @param data
*/
private static void fromStartToEnd(int[] data) {
for (int i=1; i < data.length; i++) {
int value = data[i];
int[] tmp = new int[2];
int change = i;
for (int j=0; j < i; j++) {
if(value >= data[j]) {
continue;
}
int index = j%2;
if (change == i) {
tmp[Math.abs(index-1)] = data[j];
change = j;
}
tmp[index] = data[j+1];
if (0 == index) {
data[j+1] = tmp[index+1];
} else {
data[j+1] = tmp[index-1];
}
}
data[change] = value;
}
}
}