• 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
    

  • 相关阅读:
    MySQL的count函数注意点
    case when语句的报错问题
    redis的主从搭建与sentinel高可用服务的搭建
    解析范式(1NF-4NF)
    对SQL语言的相关学习
    ASP.NET Core MVC+EF Core项目实战
    ASP.NET Core +Highchart+ajax绘制动态柱状图
    tab页卡效果!
    今天我注册了迅雷快传
    触发器学习笔记(:new,:old用法)
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501276.html
Copyright © 2020-2023  润新知