1. 程式人生 > 實用技巧 >十大排序-折半插入排序

十大排序-折半插入排序

折半插入排序
空間複雜度O(1)
時間複雜度最好情況是O(nlogn) 最壞情況是O(n²),平均為O(n²)
與直接插入排序相比,折半插入排序在查詢插入位置上面所花的時間大大減少
比較次數減少了,但是移動次數沒變
對於資料量不大的排序表,能表現很好的效能
穩定的排序方法

#include<iostream>
using namespace std;
#include<vector>

void BinaryInsrtionSort(int a[],int n)
{
    int mid;    
    for(int i = 1 ; i < n; i++)
    {
        int low = 0;
        int high = i - 1;
        int temp = a[i];
        while(low <= high)
        {
            mid = (low + high) / 2;
            if(a[i] < a[mid])
                high = mid - 1;                 //最後輸出為從小到大,所以插入的位置是high+1
            else 
                low = mid + 1;
        }
        for(int j = i - 1; j >= high + 1 ; j--) //high後面的後移
            a[j+1] = a[j];
        a[high + 1] = temp;
    }
}


void BinaryInsrtionSort2(vector<int> &a)
{
    int mid;    
    for(int i = 1 ; i < a.size(); i++)
    {
        int low = 0;
        int high = i - 1;
        int temp = a[i];
        while(low <= high)
        {
            mid = (low + high) / 2;
            if(a[i] < a[mid])
                high = mid - 1;                 
            else 
                low = mid + 1;
        }
        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)
{
    int mid;    
    for(int i = 1 ; i < n; i++)
    {
        int low = 0;
        int high = i - 1;
        int temp = a[i];
        while(low <= high)
        {
            mid = (low + high) / 2;
            if(a[i] > a[mid])
                high = mid - 1;                 
            else 
                low = mid + 1;
        }
        for(int j = i - 1; j >= high + 1 ; j--) 
            a[j+1] = a[j];
        a[high + 1] = temp;
    }
}


void BinaryInsrtionSort2_lts(vector<int> &a)
{
    int mid;    
    for(int i = 1 ; i < a.size(); i++)
    {
        int low = 0;
        int high = i - 1;
        int temp = a[i];
        while(low <= high)
        {
            mid = (low + high) / 2;
            if(a[i] > a[mid])
                high = mid - 1;                 
            else 
                low = mid + 1;
        }
        for(int j = i - 1; j >= high + 1 ; j--) 
            a[j+1] = a[j];
        a[high + 1] = temp;
    }
}


int main()
{
    int a[]={8,6,4,9,7,1,2,5,0};
    int n = sizeof(a) / sizeof(a[0]);
    vector<int> arr;
    for(int i = 0 ; i < n ; i++)
    {
        arr.push_back(a[i]);
    }
    
    for(int i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
    BinaryInsrtionSort(a,n);
    for(int i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;

 	for(auto i : arr)
    {
        cout<<i<<" ";
    }
    cout<<endl;
    BinaryInsrtionSort2(arr);
    for(auto i : arr)
    {
        cout<<i<<" ";
    }
    cout<<endl;

    BinaryInsrtionSort_lts(a,n);
    for(int i=0;i<n;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;

    BinaryInsrtionSort2_lts(arr);
    for(auto i : arr)
    {
        cout<<i<<" ";
    }
    cout<<endl;
    system("pause");
    return 0;
}