Heap Sort in C++

/*
This function is an important subroutine for manipulating max-heaps. Its inputs are an array A with it's length and an index i into the array.

When maxHeapify is called, it is assumed that the binary trees rooted at it's children are max-heaps, but that A[i] may be smaller than its children, thus violating the max-heap property. The function is to let the value at A[i] "float down" in the max-heap so that the subtree rooted at index i becomes a max-heap.
*/
void maxHeapify(int A[], int length, int i) {
    int left = 2 * i;
    int right = 2 * i + 1;
    int largest = i;
   
    if (left < length && A[left] > A[largest]) {
        largest = left;
    }
   
    if (right < length && A[right] > A[largest]) {
        largest = right;
    }
   
    if (largest != i) {
        int temp = A[i];
        A[i] = A[largest];
        A[largest] = temp;
        maxHeapify(A, length, largest);
    }
}

/*
The recursive version of maxHeapify above might cause some compilers to produce inefficient code. Below is an iterative version.
*/
void maxHeapifyInLoop(int A[], int length, int i) {
    int cur = i;
    int left;
    int right;
    int largest;
    int temp;

    while (cur < length) {
        left = 2 * cur;
        right = 2 * cur + 1;
        largest = cur;
       
        if (left < length && A[left] > A[largest]) {
            largest = left;
        }
   
        if (right < length && A[right] > A[largest]) {
            largest = right;
        }
       
        if (largest == cur) break;
       
        // switch A[cur] and A[largest]
        temp = A[cur];
        A[cur] = A[largest];
        A[largest] = temp;
       
        // change current node to largest
        cur = largest;
    }
}

/*
We can use the procedure "maxHeapify" in a bottom-up manner to convert an array A[0...(n-1)], where n = length[A], into a max-heap. The elements in the subarray A[((n-1)/2+1)...(n-1)] are all leaves of the tree, and so each is a 1-element heap to begin with. The procedure "buildMaxHeap" goes through the remaining nodes of the tree and runs "maxHeapify" on each one.
*/
void buildMaxHeap(int A[], int length) {
    for (int i = (length-1)/2; i >= 0; i--) {
        maxHeapify(A, length, i);
    }
}

/*
The heapsort algorithm starts by using "buildMaxHeap" to build a max-heap on the input array A[0...(n-1)], where n = length[A]. Since the maximum element of the array is stored at the root A[0], it can be put into its correct final position by exchanging it with A[n-1]. If we now "discard" node (n-1) from the heap (by decrementing heap-size[A]), we observe that A[0...(n-2)] can easily be made into a max-heap. The children of the root remain max-heaps, but the new root element may violate the max-heap property. All that is needed to restore the maxheap property, however, is one call to maxHeapify(A, (n-1), 0), which leaves a max-heap in A[0...(n-2)]. The heapsort algorithm then repeats this process for the max-heap of size (n-1)
down to a heap of size 2.
*/
void heapSort(int A[], int length) {
    buildMaxHeap(A, length);
   
    for (int i = length - 1; i > 0; i--) {
        int temp = A[i];
        A[i] = A[0];
        A[0] = temp;
        maxHeapify(A, i, 0);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值