一个关于二叉堆排序的问题
  • 板块灌水区
  • 楼主Gnimnehs
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/12/3 17:07
  • 上次更新2023/12/3 19:43:44
查看原帖
一个关于二叉堆排序的问题
772337
Gnimnehs楼主2023/12/3 17:07

用二叉堆做了一个排序,但运行有点问题,求指点

#include<bits/stdc++.h>
#define N 100010
using namespace std;

int heap[N];
int len=0;

void put(int x){
	len++;
	int p=len;
	heap[len]=x;
	while(p!=1 && heap[p/2]>heap[p]){
		swap(heap[p/2],heap[p]);
		p=p/2;
	}
}

int main(){
	int n;
	cin>>n;
	int t;
	for(int i=1;i<=n;i++){
		cin>>t;
		put(t);
	}
	for(int i=1;i<=n;i++) cout<<heap[i]<<" ";
	return 0;
}
2023/12/3 17:07
加载中...