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

堆排序

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

void heapInsert(vector<int> &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<int> &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<int> heapSort(int a[], int n)
{
    vector<int> 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<int> 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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

vector<int> rightfunc(vector<int> v)
{
    vector<int> res;
    vector<int>::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<int> g(int size)
{
    int n = rand() % size + 1;
    vector<int> data;
    for (int i = 0; i < n; i++)
    {
        data.push_back(rand());
    }
    return data;
}

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

int main()
{
    test();
//    vector<int> 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值