排序 折半插入排序

2022-07-31 07:18:13 字數 1290 閱讀 1840

折半插入排序

空間複雜度o(1)

時間複雜度最好情況是o(nlogn) 最壞情況是o(n²),平均為o(n²)

與直接插入排序相比,折半插入排序在查詢插入位置上面所花的時間大大減少

比較次數減少了,但是移動次數沒變

對於資料量不大的排序表,能表現很好的效能

穩定的排序方法

#includeusing namespace std;

#includevoid binaryinsrtionsort(int a,int n)

for(int j = i - 1; j >= high + 1 ; j--) //high後面的後移

a[j+1] = a[j];

a[high + 1] = temp;

}}void binaryinsrtionsort2(vector&a)

for(int j = i - 1; j >= high + 1 ; j--)

a[j+1] = a[j];

a[high + 1] = temp;

}}void binaryinsrtionsort_lts(int a,int n)

for(int j = i - 1; j >= high + 1 ; j--)

a[j+1] = a[j];

a[high + 1] = temp;

}}void binaryinsrtionsort2_lts(vector&a)

for(int j = i - 1; j >= high + 1 ; j--)

a[j+1] = a[j];

a[high + 1] = temp;

}}int main()

; int n = sizeof(a) / sizeof(a[0]);

vectorarr;

for(int i = 0 ; i < n ; i++)

for(int i=0;icout

for(int i=0;icout

cout

for(auto i : arr)

cout

for(int i=0;icout

for(auto i : arr)

cout

return 0;

}

插入排序 折半插入排序

折半插入排序 binary insertion sort 直接插入排序採用順序查詢法查詢當前記錄在已排好序的序列中插入位置,這個 查詢 操作可利用 折半查詢 來實 現,由此進行的插入排序稱之為折半插入排序 binary insertion sort 演算法思想 1 將待排序的記錄存放在陣列r 1.n...

插入排序 折半插入排序

折半插入排序是基於直接插入排序的優化。直接插入排序 將第i個元素插入時,通過折半查詢的方式,來查詢第i個元素合適的位置。當0 i 1 位置上的元素都已經排序ok,現需要插入第i個元素,設其值為temp 令low 0,high i,mid high low 2。那麼temp可能插入的位置是 low h...

插入排序 折半插入排序

package sort 折半插入排序 當序列是連續儲存時,對直接插入排序進行的改進 插入位置可以更快速找到 直接插入排序a 0 用作哨兵減少條件判斷,折半插入排序不需要哨兵a 0 位置存放實際元素 public class insertsort 統一後移,空出插入位置 for j i 1 j hi...