# scala数据结构和算法-08-堆排序

1．父结点的键值总是大于或等于（小于或等于）任何一个子节点的键值。

2．每个结点的左子树和右子树都是一个二叉堆（都是最大堆或最小堆）。

## 堆的存储

package data

import scala.collection.mutable.ListBuffer

object HeapSort {
def buildHeap[T](comparator:(T,T)=>Boolean)(source:ListBuffer[T],parent:Int){
if(left(parent)>=source.length){
return
}else{
buildHeap(comparator)(source,left(parent));
}
if(right(parent)>=source.length){
return
}else{
buildHeap(comparator)(source,right(parent));
}
if(comparator(source(left(parent)),source(parent))
&&comparator(source(right(parent)),source(parent))){
if(comparator(source(left(parent)),source(right(parent)))){
val p=source(parent)
source(parent)=source(left(parent))
source(left(parent))=p
buildHeap(comparator)(source,left(parent))
}else{
val p=source(parent)
source(parent)=source(right(parent))
source(right(parent))=p
buildHeap(comparator)(source,right(parent))
}
}else if(comparator(source(left(parent)),source(parent))){
val p=source(parent)
source(parent)=source(left(parent))
source(left(parent))=p
buildHeap(comparator)(source,left(parent))
}else if(comparator(source(right(parent)),source(parent))){
val p=source(parent)
source(parent)=source(right(parent))
source(right(parent))=p
buildHeap(comparator)(source,right(parent))
}

}

def left(parent:Int)={
parent*2+1
}

def right(parent:Int)={
parent*2+2
}

def heapfye[T](comparator:(T,T)=>Boolean)(source:ListBuffer[T],parent:Int,rightIndex:Int){
if(left(parent)>rightIndex){
return
}
/*if(right(parent)>rightIndex){
return
}*/
if(left(parent)<=rightIndex&&right(parent)>rightIndex){
if(comparator(source(left(parent)),source(parent))){
val p=source(parent)
source(parent)=source(left(parent))
source(left(parent))=p
heapfye(comparator)(source,left(parent),rightIndex)
}
}else{
if(comparator(source(left(parent)),source(parent))
&&comparator(source(right(parent)),source(parent))){
if(comparator(source(left(parent)),source(right(parent)))){
val p=source(parent)
source(parent)=source(left(parent))
source(left(parent))=p
heapfye(comparator)(source,left(parent),rightIndex)
}else{
val p=source(parent)
source(parent)=source(right(parent))
source(right(parent))=p
heapfye(comparator)(source,right(parent),rightIndex)
}
}else if(comparator(source(left(parent)),source(parent))){
val p=source(parent)
source(parent)=source(left(parent))
source(left(parent))=p
heapfye(comparator)(source,left(parent),rightIndex)
}else if(comparator(source(right(parent)),source(parent))){
val p=source(parent)
source(parent)=source(right(parent))
source(right(parent))=p
heapfye(comparator)(source,right(parent),rightIndex)
}
}
}

def heapSort[T](comparator:(T,T)=>Boolean)(source:ListBuffer[T],rightIndex:Int):ListBuffer[T]={
for(i<-(1 until source.length).reverse){
val tmp=source(i)
source(i)=source(0)
source(0)=tmp;
heapfye(comparator)(source,0,i-1)
}
source
}
def main(args: Array[String]): Unit = {
val source=ListBuffer(3,1,4,9,5,8,7)
buildHeap[Int](_>_)(source,0)
println(source.mkString(","))
println(heapSort[Int](_>_)(source,source.length-1).mkString(","))
}
}

举报原因： 您举报文章：scala数据结构和算法-08-堆排序 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)