zoukankan      html  css  js  c++  java
  • sort & find

    object test {
    
      import Breaks.{break, breakable}
    
    //  def main(args: Array[String]): Unit = {
    //    val a = Array(5, 9, 2, 3, 0, -1)
    //    sort(a, 0, a.length - 1)
    //    a.foreach(println)
    //
    //    println(find(a, 49, 0, a.length - 1))
    //  }
    
    
      def sort(arr: Array[Int], leftBound: Int, rightBound: Int): Unit = {
        val tempVar = arr(leftBound)
        var leftIndex = leftBound
        var rightIndex = rightBound
    
        while (leftIndex < rightIndex) {
          // 右边开始找小的数字
          breakable({
            while (leftIndex < rightIndex) {
              if (arr(rightIndex) < tempVar) {
                arr(leftIndex) = arr(rightIndex)
                leftIndex += 1
                break()
              }
              rightIndex -= 1
            }
          })
    
          breakable({
            while (leftIndex < rightIndex) {
              if (arr(leftIndex) > tempVar) {
                arr(rightIndex) = arr(leftIndex)
                rightIndex -= 1
                break()
              }
              leftIndex += 1
            }
          })
        }
    
        arr(leftIndex) = tempVar
    
        if (leftBound < leftIndex - 1) {
          sort(arr, leftBound, leftIndex - 1)
        }
        if (leftIndex + 1 < rightBound) {
          sort(arr, leftIndex + 1, rightBound)
        }
    
      }
    
      def find(arr: Array[Int], num: Int, leftBound: Int, rightBound: Int): Int = {
        val len = rightBound - leftBound
        if (len >= 0) {
          val index = (leftBound + rightBound) / 2
          if (arr(index) > num) {
            find(arr, num, leftBound, index - 1)
          } else if (arr(index) < num) {
            find(arr, num, index + 1, rightBound)
          } else
            index
        } else {
          -1
        }
      }
    }
    
  • 相关阅读:
    制作自适应布局的模块及框架(转载)
    从今天起开始写博了
    工作中碰到的css问题解决方法
    标题写个什么好呢
    快速编写HTML(Zen conding)
    2013年1月21日记事
    opc 方面研究
    关于 部署方面研究 Visual Studio 2013
    intel AVX指令集
    关于 返回数据类型 后 加& 的作用
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/15352023.html
Copyright © 2011-2022 走看看