排序-折半插入排序

折半插入排序
空间复杂度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;
}
posted @ 2020-07-09 18:04  Akmf's_blog  阅读(136)  评论(0)    收藏  举报