scala 排序演算法 快速排序__scala

來源:互聯網
上載者:User
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 */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.