Skip to content

Commit

Permalink
插入排序(插入位置,从头至尾搜索)
Browse files Browse the repository at this point in the history
  • Loading branch information
giganticKraken committed Nov 29, 2018
1 parent e9a0aa3 commit f74a14c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions java/11_sorts/InsertionSortAdd.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}

0 comments on commit f74a14c

Please sign in to comment.