scala 排序算法 快速排序

package com.xing.hai

/**
  * Created by xxxx on 2/24/2017.
  */
object OrderQuickSort 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)

  def quickSortGetPivot(array: Array[Int], low1 :Int ,high1 :Int): Int ={

    var pivot = array(low1)   //value
    var low = low1
    var high = high1
     while(low < high){
       while ( low < high && pivot<=array(high)){
         high -= 1
       }
       //while 不成立时候 开始换值
       swapFunction(array,low,high)

       while ( low < high && pivot>=array(low)){
         low += 1
       }
       //while 不成立时候 开始换值
       swapFunction(array,low,high)
      }
    // while low = high 时候 需要重新分组
    low
 }

  def quickSort(array: Array[Int], low :Int ,high :Int): Unit ={
    if(low< high){
      val middle = quickSortGetPivot(array: Array[Int], low :Int ,high :Int)
      println(middle)
      if(middle > 0){
        quickSort(array,low ,middle -1)
        quickSort(array,middle + 1 ,high)
      }
    }

  }

  def swapFunction(array: Array[Int],low :Int ,high :Int): Unit ={
    val temp = array(high)
    array(high) =array(low)
    array(low) = temp
  }


  quickSort(sortArray,0,sortArray.length -1)
  sortArray.foreach(x => print(x + " "))
  println()

}
/*
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 */

你可能感兴趣的:(scala)