堆排序(大)

来源:互联网 发布:好用的爽肤水 知乎 编辑:程序博客网 时间:2024/05/07 01:21
#include<iostream>#include<stdio.h>using namespace std;#define MAX_ELEMENTS 200#define HEAP_FULL(n) (n==MAX_ELEMENTS-1)#define HEAP_EMPTY(n) (!n)typedef struct{int key;}element;element heap[MAX_ELEMENTS];int n=0;void push(element item,int *n){int i;if(HEAP_FULL(*n)){printf("溢出");return ;}i=++(*n);//这样做是保证啦双向的值传递。while(i!=1&&item.key>heap[i/2].key){heap[i]=heap[i/2];i/=2;}heap[i]=item;}void out(int n){int i=0;for(i=1;i<=n;i++){cout<<heap[i].key<<"\t";}}int main(){element item;item.key=20;push(item,&n);item.key=15;push(item,&n);item.key=2;push(item,&n);item.key=14;push(item,&n);item.key=10;push(item,&n);out(n);return 0;}/*1 当时一直错,知道后来想起来这个程序才知道哪里错啦。#include<iostream>#include<stdio.h>using namespace std;void swap(int *a,int*b){int l=*a;*a=*b;*b=l;}int main(){int a=10;int b=15;cout<<a<<"\t"<<b<<endl;swap(&a,&b);//注意这个取地址符号cout<<a<<"\t"<<b<<endl;return 0;}*/