forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9a0aa3
commit f74a14c
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |