【左神算法】基础班第二课(三)堆排序及对结构的应用

堆排序

#include 
#include 
using namespace std;

void heapInsert(vector &a, int value)
{
    a.push_back(value);
    int index = (int)a.size() - 1;
    while (index > 0)
    {
        int parent = (index - 1) / 2;
        
        if (a[parent] >= a[index])
        {
            break;
        }
        else
        {
            swap(a[index], a[parent]);
            index = parent;
        }
    }
}

void heapify(vector &a, int index, int size)
{
    while (index < size)
    {
        int ch = 2 * index + 1;
        
        if (ch + 1 < size && a[ch] < a[ch + 1])
        {
            ch++;
        }
        
        if (ch >= size || a[ch] <= a[index])
        {
            break;
        }
        
        swap(a[ch], a[index]);
        index = ch;
    }
}

vector heapSort(int a[], int n)
{
    vector res;
    
    for (int i = 0; i < n; i++)
    {
        heapInsert(res, a[i]);
    }
    
    for (int i = 0; i < res.size(); i++)
    {
        swap(res[0], res[res.size() - i - 1]);
        heapify(res, 0, (int)res.size() - i - 1);
    }
    return res;
}


int main()
{
    int a[9] = {57, 68, 59, 52, 72, 28, 96, 33, 24};
    
    vector res = heapSort(a, 9);
    
    for (int i = 0; i < 9; i++)
    {
        cout << res[i] << ' ';
    }
    
    return 0;
}

建堆时间复杂度:0 + log(1) + log(2) + ... + log(n - 1) = log[(n-1)!] ,当N区域无穷的时候,O(n).证明方法log(n^n)是等价无穷小。

具体证明

 

用堆找流的中位数

#include 
#include 
#include 

using namespace std;

vector f(vector v)
{
    vector maxheap;
    vector minheap, res;
    
    make_heap(maxheap.begin(), maxheap.end(), less());
    make_heap(minheap.begin(), minheap.end(), greater());
    
    for (int i = 0; i < v.size(); i++)
    {
        if (maxheap.size() * minheap.size() == 0)
        {
            maxheap.push_back(v[i]);
            push_heap(maxheap.begin(), maxheap.end(), less());
        }
        else
        {
            if (v[i] > minheap.front())
            {
                minheap.push_back(v[i]);
                push_heap(minheap.begin(), minheap.end(), greater());
            }
            else
            {
                maxheap.push_back(v[i]);
                push_heap(maxheap.begin(), maxheap.end(), less());
            }
        }
        
        
        if (minheap.size() - maxheap.size() == 2)
        {
            pop_heap(minheap.begin(), minheap.end(), greater());
            int temp = minheap.back();
            minheap.pop_back();
            maxheap.push_back(temp);
            push_heap(maxheap.begin(), maxheap.end(), less ());
        }
        else if (minheap.size() - maxheap.size() == -2)
        {
            pop_heap(maxheap.begin(), maxheap.end(), less());
            int temp = maxheap.back();
            maxheap.pop_back();
            minheap.push_back(temp);
            push_heap(minheap.begin(), minheap.end(), greater ());
        }
        if (minheap.size() > maxheap.size())
        {
            res.push_back(minheap.front());
        }
        else
        {
            res.push_back(maxheap.front());
        }
    }
    
    return res;
}

vector rightfunc(vector v)
{
    vector res;
    vector::iterator it = v.begin();
    for (int i = 1; i <= v.size(); i++)
    {
        it++;
        sort(v.begin(), it);
        res.push_back(v[(i - 1) / 2]);
    }
    return res;
}

vector g(int size)
{
    int n = rand() % size + 1;
    vector data;
    for (int i = 0; i < n; i++)
    {
        data.push_back(rand());
    }
    return data;
}

void test()
{
    int N = 5000;
    
    while (N--)
    {
        vector data = g(3000);
        
        vector res1 = f(data);
        vector res2 = rightfunc(data);
        
        if (res1 == res2)
        {
            cout << N << endl;
        }
        else
        {
            cout << "error" << endl;
            return;
        }
    }
    cout << "OK" << endl;
}

int main()
{
    test();
//    vector v, res;
//
//    int n, x;
//    cin >> n;
//
//    for (int i = 0; i < n; i++)
//    {
//        cin >> x;
//        v.push_back(x);
//    }
//
//    res = f(v);
//
//    for (int i = 0; i < res.size(); i++)
//    {
//        cout << res[i] << ' ';
//    }
//    putchar(10);
    return 0;
}

 

你可能感兴趣的:(算法练习)