美文网首页
2_7堆排序

2_7堆排序

作者: X_Y | 来源:发表于2017-09-06 16:51 被阅读2次

循环实现

class HeapSort {
public:
    void swap(int* A,int i,int j)
    {
        int temp;
        temp=A[i];
        A[i]=A[j];
        A[j]=temp;
    }
    // 调整堆, adjusting_node是当前待调整的节点,last_node是最后一个节点
    void adjust_heap(int* A, int adjusting_node, int last_node)
    {
       int parent =  adjusting_node, child = 2 * adjusting_node + 1;
       int curr_value = A[adjusting_node];
       while(child <= last_node){
           if(child < last_node && A[child] < A[child+1]){
               ++child;
           }
           if(curr_value < A[child]){
               A[parent] = A[child];
               parent = child;
               child = 2*parent + 1;
           }
           else{
               break;
           }
       }
       A[parent] = curr_value;
    }
    int* heapSort(int* A, int n) {
        // write code here
        // 生成堆,从最后节点的parent开始
        if(n<2){
            return A;
        }
        for(int i=n/2-1; i>=0; --i){
            adjust_heap(A, i, n-1);
        }
        // 每次A[0]和最后的节点交换,然后调整堆 
        for(int i=n-1; i>0; --i){
            swap(A, i, 0);
            adjust_heap(A, 0, i-1);
        }
        return A;
    }
};

递归实现

class HeapSort {
public:
    void swap(int* A,int i,int j)
    {
        int temp;
        temp=A[i];
        A[i]=A[j];
        A[j]=temp;
    }
    // 递归调整
    void adjust_heap(int* A, int curr_node, int last_nod)
    {
        int parent = curr_node;
        int l_child = 2*parent+1;
        int r_child = 2*parent+2;
        if(l_child > last_nod){
            return;
        }
        if(r_child <= last_nod){
            if(A[parent] < A[l_child]){
               swap(A, parent, l_child); 
            }
            if(A[parent] < A[r_child]){
                swap(A, parent, r_child);
            }
            adjust_heap(A, l_child, last_nod);
            adjust_heap(A, r_child, last_nod);
        }
        if(last_nod == l_child){
            if(A[parent] < A[l_child]){
               swap(A, parent, l_child); 
            }
            adjust_heap(A, l_child, last_nod);
        }
    }
    int* heapSort(int* A, int n) {
        // write code here
        for(int i=n/2-1; i>=0; --i){
            adjust_heap(A, i, n-1);
        }
        for(int i=n-1; i>0; --i){
            swap(A, i, 0);
            adjust_heap(A, 0, i-1);
        }
        return A;
    }
};

相关文章

  • 2_7堆排序

    循环实现 递归实现

  • 堆排序

    目录 1.堆排序介绍 2.堆排序图文说明 3.堆排序的时间复杂度和稳定性 4.堆排序实现 堆排序介绍 堆排序(He...

  • 堆排序---基础篇

    本文主要介绍堆排序的一些基本过程和分析。 大纲 堆排序简介 堆排序代码实现 1. 堆排序简介 1.1 堆排序的存储...

  • 堆和堆排序

    最小K个数 堆排序 堆排序

  • JS实现堆排序

    原理 堆排序原理 实现 说明 堆排序对大文件很有效 堆排序是不稳定排序

  • iOS算法总结-堆排序

    iOS算法总结-堆排序 iOS算法总结-堆排序

  • 堆排序

    转载:图解排序算法(三)之堆排序 预备知识 堆排序 堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选...

  • 排序

    原创 堆排序: 使用visit数组从本质出发获取大顶堆排序。

  • 堆排序

    堆排序

  • C++基础入门之模板堆排序(上):模板上的list的创造与操作

    整段源码链接C++的模板元堆排序 要点 组建数据结构list 组建对list的各种基本操作 堆排序中组建堆排序个个...

网友评论

      本文标题:2_7堆排序

      本文链接:https://www.haomeiwen.com/subject/fadvjxtx.html