forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryInsertSort.java
More file actions
43 lines (32 loc) · 666 Bytes
/
BinaryInsertSort.java
File metadata and controls
43 lines (32 loc) · 666 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.csdhsm.sort;
/**
* @Title: BinaryInsertSort.java
* @Package: com.csdhsm.sort
* @Description 折半插入排序
* @author Han
* @date 2016-4-3 上午10:56:09
* @version V1.0
*/
public class BinaryInsertSort {
public void sort(int[] arr,int len){
for(int i=1;i<len;i++){
int low = 0;
int high = i-1;
int k = arr[i];
//从mid开始查找应该插入的位置
while(low <= high){
//计算mid的位置
int mid = (low + high)/2;
if(arr[mid] < k){
low ++;
}else{
high --;
}
}
for(int j=i-1;j>=low;j--){
arr[j+1] = arr[j];
}
arr[low] = k;
}
}
}