插入排序(插入位置,从头至尾搜索)
This commit is contained in:
parent
e9a0aa3dba
commit
f74a14c60d
48
java/11_sorts/InsertionSortAdd.java
Normal file
48
java/11_sorts/InsertionSortAdd.java
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user