scala 排序算法 堆排序

package com.xing.hai

/**
  * Created by xxxx on 2/23/2017.
  */
object OrderHeapSort extends App{

  val sortArray = Array(49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,43)
  //val  sortArray = Array(46,79,56,38,40,84 )
  val arrayLength = sortArray.length

  for(i<- 0 until arrayLength){
    buildMaxHeapTree(sortArray,arrayLength -1 - i)
    //每次loop 后用array(0) 最大值跟最后一个元素互换 ,后面循环时候排序最后一个元素

    swapFunction(sortArray,0 ,arrayLength - 1 - i)

    sortArray.foreach(x => print(x + " "))
    println("arrayLength - 1 - i = " + (arrayLength - 1 - i))
  }

  def buildMaxHeapTree(array:Array[Int],lastIndex :Int): Unit ={

    //根据最后一个节点计算父节点,最有一个节点可能是左节点 也可能是右节点
    //val parent = (lastIndex -1) / 2

    for(parent <- Range((lastIndex -1) / 2 , -1,-1) if lastIndex > 0 ){

      //根据父节点计算左叶子节点
      val leftChild = 2*parent + 1

      //定义一个变量等于leftchild 索引 ,用于跟父节点做swap 交换
      var swapChild = leftChild

      //根据左边节点计算右边节点
      val rightChild = leftChild + 1

      //判断如果右边节点存在,那么 比较左右几点大小
      if(rightChild <=lastIndex && array(leftChild) < array(rightChild)){
        swapChild +=1
      }

      //判断父节点跟较大左右节点中的数据大小
      if(array(parent) < array(swapChild)){
        swapFunction(array,parent,swapChild)
      }
    }

  }

  def swapFunction(array: Array[Int],parent:Int,child :Int): Unit ={

    val  temp  = array(parent)
    array(parent) = array(child)
    array(child) = temp

  }



}


43 49 65 38 76 64 62 97 78 34 34 13 53 51 27 49 98 54 56 17 18 23 12 15 35 25 5 4 99 arrayLength - 1 - i = 28
4 43 65 49 76 64 62 38 78 34 34 35 53 51 27 49 97 54 56 17 18 23 12 15 13 25 5 98 99 arrayLength - 1 - i = 27
5 4 65 43 76 64 62 49 78 34 34 35 53 51 27 49 38 54 56 17 18 23 12 15 13 25 97 98 99 arrayLength - 1 - i = 26
25 5 65 4 76 64 62 49 43 34 34 35 53 51 27 49 38 54 56 17 18 23 12 15 13 78 97 98 99 arrayLength - 1 - i = 25
13 25 65 56 5 64 62 49 4 34 34 35 53 51 27 49 38 54 43 17 18 23 12 15 76 78 97 98 99 arrayLength - 1 - i = 24
15 56 13 25 34 64 62 49 54 5 34 35 53 51 27 49 38 4 43 17 18 23 12 65 76 78 97 98 99 arrayLength - 1 - i = 23
12 56 15 54 34 13 62 49 25 18 34 35 53 51 27 49 38 4 43 17 5 23 64 65 76 78 97 98 99 arrayLength - 1 - i = 22
23 56 12 54 34 53 15 49 43 18 34 35 13 51 27 49 38 4 25 17 5 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 21
5 23 53 54 34 12 51 49 43 18 34 35 13 15 27 49 38 4 25 17 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 20
17 5 53 23 34 35 51 49 43 18 34 12 13 15 27 49 38 4 25 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 19
25 49 17 5 34 35 51 23 43 18 34 12 13 15 27 49 38 4 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 18
4 49 25 49 34 35 17 5 43 18 34 12 13 15 27 23 38 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 17
5 4 35 49 34 25 27 38 43 18 34 12 13 15 17 23 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 16
23 5 35 4 34 25 27 38 43 18 34 12 13 15 17 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 15
17 23 35 5 34 25 27 38 4 18 34 12 13 15 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 14
15 17 35 23 34 25 27 5 4 18 34 12 13 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 13
13 34 15 23 17 25 27 5 4 18 34 12 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 12
12 13 27 23 34 25 15 5 4 18 17 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 11
17 12 27 23 13 25 15 5 4 18 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 10
13 23 17 12 18 25 15 5 4 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 9
4 23 13 12 18 17 15 5 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 8
5 4 17 12 18 13 15 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 7
15 5 17 12 4 13 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 6
13 12 15 5 4 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 5
4 12 13 5 15 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 4
5 12 4 13 15 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 3
4 5 12 13 15 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 2
4 5 12 13 15 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 1
4 5 12 13 15 17 18 23 25 27 34 34 35 38 43 49 49 51 53 54 56 62 64 65 76 78 97 98 99 arrayLength - 1 - i = 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值