MCPcopy Create free account
hub / github.com/142vip/408CSFamily / BinaryInsertSort

Function BinaryInsertSort

code/ds/BinaryInsertSort.cpp:2–35  ·  view source on GitHub ↗

折半查找

Source from the content-addressed store, hash-verified

1// 折半查找
2void BinaryInsertSort(ElemType Arr[],int n){
3 int i,j,lowIndex,highIndex,midIndex;
4
5 for(i=2;j<=n;i++){
6 // 将待排序的元素暂存在Arr[0]上
7 Arr[0]=Arr[i];
8
9 lowIndex=1; // 左侧子表 折半查找起始位置
10 highIndex=i-1; // 左侧子表 折半查找结束位置
11 while(lowIndex<=highIndex){
12
13 // 左侧有序子表的中间位置角标
14 midIndex=(lowIndex+heightIndex)/2;
15
16 if(Arr[midIndex].key>Arr[0].key){
17 // 小于中间元素,插入位置在子表左侧
18 highIndex=mid-1
19 }else{
20 // 大于或者等于中间元素,插入位置在子表右侧
21 lowIndex=midIndex+1;
22 }
23 }
24
25 // 跳出循环需要(lowIndex>heightIndex),
26 // 说明待插入位置的角标在heightIndex之后,为 heightIndex+1,此时需要将(heightIndex,i)之间的所有元素后移
27
28 for(j=i-1;j>highIndex;--j){
29 Arr[j+1]=Arr[j]
30 }
31
32 // 后移完成后,将元素Arr[0]赋值到位置(highIndex+1)上
33 Arr[highIndex+1]=Arr[0]
34 }
35}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected