scala实现几大排序算法

package Sort

/**
  * Created by legotime on 2016/5/14.
  */
class ScalaSort {
  //------------------------产生随机序列------------------------------------------

  //产生一组随机数,内部元素可能相同
  def RandomList(n:Int) = Seq.fill(n)(scala.util.Random.nextInt(n))

  //产生一组随机数,内部元素不相同
  def RandomDiffList(n:Int): List[Int] ={
   var resultList:List[Int] = Nil
    while(resultList.length<n){
      val tempNum = (new scala.util.Random).nextInt(n)
      if(! resultList.contains(tempNum))
        resultList = resultList:::List(tempNum)
    }
    resultList
  }
  //-----------------------------排序算法-----------------------------------------
  //implicit  def ListToArray(List1:List[Int]) = List1 toArray
  //implicit def ArrayToList(Array2:Array[Int]) = Array2 toList

  //直接插入排序(Straight Select Sorting)
  //scala 中有直接的insert函数,和直接插入排序的思想一样,在此省略改插入法



  //希尔排序(shell sort)
  def shellSort(SortList:List[Int]):List[Int] ={
    val cpSortList = SortList.toArray
    var d = cpSortList.length
    while( d > 1){
      d = math.floor(d/2).toInt
      for(i <- 0 to d){
        //小组内排好序
        for (j <- Range(i,cpSortList.length,d)){
          var minIndex = j
          for (o <- Range(j+d,cpSortList.length,d)){
            if(cpSortList(minIndex) > cpSortList(o))//cpSortList(minIndex) < cpSortList(j) 从大到小
              minIndex = o
          }
          val temp = cpSortList(j)
          cpSortList(j) =  cpSortList(minIndex)
          cpSortList(minIndex) = temp
        }
      }

    }
    cpSortList.toList
  }

  //简单选择排序(Selection sort)
  def selectSort(SortList:List[Int]):List[Int] ={
    val cpSortList = SortList.toArray
    for (i <- 0 until cpSortList.length){
      var minIndex = i
      //找到当前循环的最小值
      for (j <- i+1 until  cpSortList.length){
        if(cpSortList(minIndex) > cpSortList(j))//cpSortList(minIndex) < cpSortList(j) 从大到小
          minIndex = j
      }
      val temp = cpSortList(i)
      cpSortList(i) =  cpSortList(minIndex)
      cpSortList(minIndex) = temp
    }
    cpSortList toList
  }

  //冒泡排序(Bubble Sort)
  def bubleSort(SortList:List[Int]):List[Int] ={
    val cpSortList = SortList.toArray
    for(i <- 0 until cpSortList.length-1; j <- 0 until cpSortList.length-1-i){
      if(cpSortList(j) > cpSortList(j+1)){
        val tmp = cpSortList(j)
        cpSortList(j) = cpSortList(j+1)
        cpSortList(j+1) = tmp
      }
    }
    cpSortList toList
  }

  //快速排序把(quickSort)
  //《Scala By Example》的一个列子
  def quickSort(SortList: Array[Int]): Array[Int]  = {
   if(SortList.length <= 1) SortList
  else{
    val pivot = SortList(SortList.length / 2)
    Array.concat(
      quickSort(SortList filter (pivot >)),
      SortList filter (pivot ==),
      quickSort(SortList filter (pivot <))
     )
   }
  }




  //归并排序
  def mergeSort(SortList: List[Int]): List[Int] = {
    def merge(a: List[Int], b: List[Int]): List[Int] = (a,b) match {
      case (Nil, _) => b
      case (_, Nil) => a
      case (x::xs, y::ys) =>
        if(x <= y) x :: merge(xs, b)
        else y :: merge(a, ys)
    }
    if(SortList.length == 1) SortList
    else{
      val (first, second) = SortList.splitAt(SortList.length/2)
      merge(mergeSort(first), mergeSort(second))
    }
  }
  


}
object ScalaSort {
  def main(args: Array[String]) {
    val randomSort = new ScalaSort()
    val rdmList =  randomSort RandomDiffList(10)
    //val rdmNum = scala.util.Random.nextInt(10)
    println("排序前:"+rdmList)
    println("直接插入排序:"+randomSort.selectSort(rdmList))
    println("希尔排序:"+randomSort.shellSort(rdmList))
    println("冒泡排序:"+randomSort.bubleSort(rdmList))
    println("快速排序:"+randomSort.quickSort(rdmList.toArray).toList)
    println("归并排序:"+randomSort.mergeSort(rdmList))
    //排序前:List(3, 9, 8, 7, 0, 1, 4, 5, 6, 2)
    //直接插入排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    //希尔排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    //冒泡排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    //快速排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    //归并排序:List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值