堆排序模板

// ConsoleApplication9.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include "pch.h"
#include<stdio.h>

#include<string.h>

void swap(int &x, int &y)

{

    int a;

    a = x;

    x = y;

    y = a;

}

void HeapAdjust(int *a, int i, int size)  //调整堆

{

    int lchild = 2 * i;       //i的左孩子节点序号
    int rchild = 2 * i + 1;     //i的右孩子节点序号
    int max = i;            //临时变量
    if (i <= size / 2)          //如果i是叶节点就不用进行调整
    {
        if (lchild <= size && a[lchild] > a[max])
        {
            max = lchild;
        }
        if (rchild <= size && a[rchild] > a[max])
        {
            max = rchild;
        }
        if (max != i)
        {
            swap(a[i], a[max]);
            HeapAdjust(a, max, size);    //避免调整之后以max为父节点的子树不是堆
        }
    }

}

void BuildHeap(int *a, int size)    //建立堆

{

    int i;
    for (i = size / 2; i >= 1; i--)    //非叶节点最大序号值为size/2
    {
        HeapAdjust(a, i, size);
    }

}

void HeapSort(int *a, int size)    //堆排序

{

    int i;
    BuildHeap(a, size);
    for (i = size; i >= 1; i--)
    {
        //cout<<a[1]<<" ";
        swap(a[1], a[i]);           //交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到最后面
          //BuildHeap(a,i-1);        //将余下元素重新建立为大顶堆
        HeapAdjust(a, 1, i - 1);      //重新调整堆顶节点成为大顶堆
    }

}

int main(int argc, char *argv[])

{

    //int a[]={0,16,20,3,11,17,8};
    int a[100];
    int size;
    while (scanf("%d", &size) != EOF && size > 0)
    {
        int i;
        for (i = 1; i <= size; i++)
            scanf("%d", &a[i]);
        HeapSort(a, size);
        for (i = 1; i <= size; i++)
            printf("%d,", a[i]);

        printf("\n");
    }
    return 0;

}

#include <stdio.h>

struct Table {
	int value;
	int idx;
}table[3000];
struct Table* heap[3000];
int heapnum;

int first_is_better(int a, int b) {
	return heap[a]->value > heap[b]->value;
}
void swap(int a, int b) {
	struct Table* tmp = heap[a];
	heap[a] = heap[b];
	heap[b] = tmp;

	heap[a]->idx = a;
	heap[b]->idx = b;
}
void move_up(int idx) {
	int up = (idx-1) / 2;
	while (up >= 0) {
		if (first_is_better(idx, up)) {
			swap(up, idx);
		}
		else {
			break;
		}
		idx = up;
		up = (idx - 1) / 2;
	}
}

void move_down(int idx) {
	int left = idx * 2 + 1;
	int right = left + 1;
	int down = left;
	while (left < heapnum) {
		if (right < heapnum && first_is_better(right, left)) {
			down = right;
		}
		if (first_is_better(down, idx)) {
			swap(down, idx);
		}
		else {
			break;
		}
		idx = down;
		left = down * 2 + 1;
		right = left + 1;
		down = left;
	}
}
void addheap(int idx)
{
	heap[heapnum] = &table[idx];
	heapnum++;
	move_up(heapnum-1);
}

void adjust(int idx) {
	move_up(idx);
	move_down(idx);
}

void deletenode(int idx) {
	heap[idx] = heap[heapnum - 1];
	heap[idx]->idx = idx;
	heapnum--;
	adjust(idx);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值