教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

堆排序、快速排序、基数排序(静态链表)输出一组数组(2)

来源:网络收集 时间:2026-08-31
导读: } } Array[l]=TempRecord; return l; } template class MaxHeap //堆类型 { public: T *heapArray; intCurrentSize; MaxHeap(T *Array,int n); voidBuildHeap(); intLeftChild(intpos); voidSiftdown(int left); T

}

}

Array[l]=TempRecord;

return l;

}

template class MaxHeap

//堆类型

{ public: T *heapArray; intCurrentSize;

MaxHeap(T *Array,int n); voidBuildHeap(); intLeftChild(intpos); voidSiftdown(int left); T&RemoveMax();

};

template

MaxHeap::MaxHeap(T *Array,int n)

{

//构造函数

}

if(n<=0)

return;

CurrentSize=n; heapArray=Array; BuildHeap();

template

void MaxHeap::BuildHeap() { }

template

intMaxHeap::LeftChild(intpos){ }

template

void MaxHeap::Siftdown(int left) {

//从left开始想向下筛选

return 2*pos+1;

//返回左孩子

for(int i=CurrentSize/2-1;i>=0;i--)

Siftdown(i);

//建堆

}

int i=left; int j=LeftChild(i); T temp=heapArray[i]; while(j

heapArray[i]=temp;

if((j

j++;

if(temp

else break;

heapArray[i]=heapArray[j]; i=j;

j=LeftChild(j);

template

T&MaxHeap::RemoveMax() //从堆顶删除最大值 {

if(CurrentSize==0) {

//判断非空

cout<<\ exit(1);

}

else{ //交换并向下筛选

T temp;

temp=heapArray[0];

heapArray[0]=heapArray[--CurrentSize]; heapArray[CurrentSize]=temp; if(CurrentSize>1)

Siftdown(0);

returnheapArray[CurrentSize];

}

}

template void sort(Record Array[],int n)

//堆排序

{

MaxHeapmax_heap=MaxHeap(Array,n);

最大记录 for(int i=0; i

max_heap.RemoveMax();

}

//依次找出

class Record{ public: };

classstaticqueue{ public: };

template r为基数

voidradixsort(Record *arra,intn,intd,int r){

inti,first=0;

staticqueue *queue=new staticqueue[r]; for(i=0;i

//静态链实现基数排序,n为数组长度,d为排序码个数,

int head; int tail;

int key; int next;

堆排序、快速排序、基数排序(静态链表)输出一组数组(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/596330.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)