堆排序

  实现了一下堆排序,几个小部分包括自顶向下调整、自下向上调整、插入操作、删除操作、数组建堆、堆排序,使用了函数指针来支持大顶和小顶两种模式

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

void swap(int &a, int &b) {
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
}

// Use the array hold the heap struct, if a node have index i(
// index starts from 0), so his parent's index will be (i-1) / 2
// and his left child's index will be 2i + 1, his right child's 
// index will be 2i + 2


bool large(int a, int b) {
    return a > b;
}

bool small(int a, int b) {
    return a < b;
}

typedef bool(*COMPARE)(int, int);

// Adjust heap up, if use min heap compare argument should be small function,
// otherwise, the large function should be used
void heapAdjustUp(int a[], int n, COMPARE compare) {
    while (n && ((n - 1)/2) >= 0) {
        if (compare(a[n], a[(n-1)/2])) {
            swap(a[n], a[(n-1)/2]);
            n = (n-1) / 2;
        } else
            break;
    }    
}

// Adjust heap down, if use min heap compare argument should be small function,
// otherwise, the large function should be used
void heapAdjustDown(int a[], int n, int index, COMPARE compare) {
    // From top to down adjust heap
    while (index < n) {
        int lchild = 2 * index + 1; 
        int rchild = 2 * index + 2;
        if (lchild > n)
            return;
        if (rchild > n) {
            if (compare(a[lchild], a[index]))
                swap(a[index], a[lchild]); 
            return;
        }
        int targetIndex = compare(a[index], a[lchild]) ? 
            (compare(a[index], a[rchild]) ? index : rchild) :
            (compare(a[lchild], a[rchild]) ? lchild : rchild);
        if (targetIndex != index)
            swap(a[index], a[targetIndex]);
        else
            return;
        index = targetIndex;
    }
}

// Insert to heap, the array's capacity should be enough
void heapInsert(int a[], int n, int num, COMPARE compare) {
    a[n] = num;
    heapAdjustUp(a, n, compare);
}

// Delete element from a heap
void headErase(int a[], int n, int index, COMPARE compare) {
    swap(a[n], a[index]);
    heapAdjustDown(a, n-1, index, compare);
} 


// Make a heap from a heap
// If use min heap compare argument should be small function,
// otherwise, the large function should be used
void makeHeap(int a[], int n, COMPARE compare) {
    if (n == 0 || n == 1)
        return;
    n -= 1;
    for (int i = (n-1)/2; i >= 0; i--)
        heapAdjustDown(a, n, i, compare);
}

// heap_sort
void heapSort(int a[], int n) {
    // First we should make a heap
    makeHeap(a, n, large);
    // In order to sort the array, delete the heap top element, and put it in
    // the array's back, adjust the heap's size and elements remained
    for (int i = 1; i < n; i++)
        headErase(a, n-i, 0, large);
}

int main() {
    int a[] = {10, 9, 11, 15, 7, 2, 8, 17};
    for (int i = 0; i < 8; ++i) 
        cout << a[i] << " ";
    cout << endl;
    heapSort(a, 8);
    for (int i = 0; i < 8; ++i) 
        cout << a[i] << " ";
    cout << endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值