您的位置:首页 > 编程语言 > C语言/C++

Heap Sort in C++

2006-04-29 17:06 369 查看
/*
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);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息