###### 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(","))
}
}

hxpjava1

#### 数据结构之DFS与BFS实现

2016-06-26 15:51:59

#### Spark TopK 问题解决-使用最小堆

2016-02-24 17:03:08

#### scala 排序算法 直接插入排序

2017-02-22 19:26:36

#### Scala 的数据结构

2015-07-06 10:52:19

#### 用scala语言实现并行堆排序(top k)

2015-09-25 12:27:04

#### scala 排序算法 堆排序

2017-02-23 21:32:10

#### Scala练习-堆排序

2017-06-26 19:29:50

#### 堆排序及top K 算法

2015-07-10 16:52:40

#### scala 九大排序算法

2017-07-05 15:06:09

#### Java堆排序，取得前TopN个数

2014-04-29 12:28:41

## 不良信息举报

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