zoukankan      html  css  js  c++  java
  • 堆排序理解 完整代码

    /*
    <转自 http://www.wutianqi.com/?p=1820 >
    
    
    自我修改
    
    * Note:   堆排序(Heap Sort)
    */
    #include <iostream>
    using namespace std;
    
    // 输出当前堆的排序状况
    void PrintArray(int data[], int size)
    {
        for (int i=1; i<=size; ++i)
            cout <<data[i]<<"  ";
        cout<<endl;
    }
    
    // 堆化,保持堆的性质
    // MaxHeapify让a[i]在最大堆中"下降",
    // 使以i为根的子树成为最大堆
    void MaxHeapify(int *a, int i, int size)
    {
        int lt = 2*i, rt = 2*i+1;
        int largest;
        if(lt <= size && a[lt] > a[i])   // 符号修改后形成的最小堆
            largest = lt;
        else
            largest = i;
        if(rt <= size && a[rt] > a[largest])
            largest = rt;
        if(largest != i)
        {
            int temp = a[i];
            a[i] = a[largest];
            a[largest] = temp;
            MaxHeapify(a, largest, size);
        }
    }
    
    // 建堆
    // 自底而上地调用MaxHeapify来将一个数组a[1..size]变成一个最大堆
    //
    void BuildMaxHeap(int *a, int size)
    {
        for(int i=size/2; i>=1; --i)
            MaxHeapify(a, i, size);
    }
    
    // 堆排序
    // 初始调用BuildMaxHeap将a[1..size]变成最大堆
    // 因为数组最大元素在a[1],则可以通过将a[1]与a[size]互换达到正确位置
    // 现在新的根元素破坏了最大堆的性质,所以调用MaxHeapify调整,
    // 使a[1..size-1]成为最大堆,a[1]又是a[1..size-1]中的最大元素,
    // 将a[1]与a[size-1]互换达到正确位置。
    // 反复调用Heapify,使整个数组成从小到大排序。
    // 注意: 交换只是破坏了以a[1]为根的二叉树最大堆性质,它的左右子二叉树还是具备最大堆性质。
    //        这也是为何在BuildMaxHeap时需要遍历size/2到1的结点才能构成最大堆,而这里只需要堆化a[1]即可。
    void HeapSort(int *a, int size)
    {
        BuildMaxHeap(a, size);
        PrintArray(a, size);
        int len = size;
        for(int i=size; i>=2; --i)
        {
            int temp = a[1];
            a[1] = a[i];
            a[i] = temp;
            len--;
            MaxHeapify(a, 1, len);
            cout << "中间过程:";
            PrintArray(a, size);
        }
    
    }
    
    int main()
    {
        int size;
        int arr[100];
        cout << "Input the num of elements:
    ";
        cin >> size;
        cout << "Input the elements:
    ";
        for(int i=1; i<=size; ++i)
            cin >> arr[i];
        cout << endl;
        HeapSort(arr, size);
        cout << "最后结果:";
        PrintArray(arr, size);
    }
    /*
    最大堆
    Input the num of elements:
    10
    Input the elements:
    50 36 41 19 23 4 20 18 12 22
    
    50  36  41  19  23  4  20  18  12  22
    中间过程:41  36  22  19  23  4  20  18  12  50
    中间过程:36  23  22  19  12  4  20  18  41  50
    中间过程:23  19  22  18  12  4  20  36  41  50
    中间过程:22  19  20  18  12  4  23  36  41  50
    中间过程:20  19  4  18  12  22  23  36  41  50
    中间过程:19  18  4  12  20  22  23  36  41  50
    中间过程:18  12  4  19  20  22  23  36  41  50
    中间过程:12  4  18  19  20  22  23  36  41  50
    中间过程:4  12  18  19  20  22  23  36  41  50
    最后结果:4  12  18  19  20  22  23  36  41  50
    
    Process returned 0 (0x0)   execution time : 2.411 s
    Press any key to continue.
    
    */
    /*
    最小堆
    Input the num of elements:
    10
    Input the elements:
    50 36 41 19 23 4 20 18 12 22
    
    4  12  20  18  22  41  50  36  19  23
    中间过程:12  18  20  19  22  41  50  36  23  4
    中间过程:18  19  20  23  22  41  50  36  12  4
    中间过程:19  22  20  23  36  41  50  18  12  4
    中间过程:20  22  41  23  36  50  19  18  12  4
    中间过程:22  23  41  50  36  20  19  18  12  4
    中间过程:23  36  41  50  22  20  19  18  12  4
    中间过程:36  50  41  23  22  20  19  18  12  4
    中间过程:41  50  36  23  22  20  19  18  12  4
    中间过程:50  41  36  23  22  20  19  18  12  4
    最后结果:50  41  36  23  22  20  19  18  12  4
    
    Process returned 0 (0x0)   execution time : 2.795 s
    Press any key to continue.
    
    */
  • 相关阅读:
    Python之美[从菜鸟到高手]--Python垃圾回收机制及gc模块详解
    linux-memory-buffer-vs-cache
    MYSQL----myownstars(102)
    win10系统调用架构分析
    on io scheduling again
    Java并发编程
    elixir-lang
    mydumper工作原理, seconds_behind_master的陷阱和pt-heartbeat (102)
    深入理解JavaScript系列+ 深入理解javascript之执行上下文
    我们应该如何去了解JavaScript引擎的工作原理 系列
  • 原文地址:https://www.cnblogs.com/locojyw/p/3750600.html
Copyright © 2011-2022 走看看