Scala代码冒泡循环排序

问题描述 投票:0回答:3
object BubbleSort {
  def main(args : Array[String]) : Unit = {
    bubbleSort(Array(50,33,62,21,100)) foreach println
  }
  def bubbleSort(a:Array[Int]):Array[Int]={
    for(i<- 1 to a.length-1){
      for(j <- (i-1) to 0 by -1){
        if(a(j)>a(j+1)){
          val temp=a(j+1)
          a(j+1)=a(j)
          a(j)=temp
        }
      }
    }
    a
  }
}

我有上面的代码据说在Scala中实现冒泡排序。它正在对主要的给定数字进行排序,但它是一个很好实现的冒泡排序算法吗?此代码行在伪代码中的含义是:for(j < - (i-1)to 0 by -1){

我无法理解。

谢谢你的帮助

algorithm scala sorting
3个回答
2
投票

您发布的示例基本上是在Scala中进行冒泡排序的命令式或Java方式,这也不错但是在Scala中违反了函数式编程的目的。相同的代码可以编写如下的排序器(基本上将两个for循环组合在一起)一行并在源长度上做一个范围)

 def imperativeBubbleSort[T <% Ordered[T]](source: Array[T]): Array[T] = {
    for (i <- 0 until source.length - 1; j <- 0 until source.length - 1 - i) {
      if (source(j) > source(j + 1)) {
         val temp = source(j)
         source(j) = source(j + 1)
         source(j + 1) = temp
      }
    }
    source
   }

Scala Flavor of bubble sort can be different and simple example is below
(basically usage of Pattern matching..)

    def bubbleSort[T <% Ordered[T]](inputList: List[T]): List[T] = {
      def sort(source: List[T], result: List[T]) = {
          if (source.isEmpty) result
          else bubble(source, Nil, result)
      }


    def bubble(source: List[T], tempList: List[T], result: List[T]): List[T] = source match {
          case h1 :: h2 :: t =>
              if (h1 > h2) bubble(h1 :: t, h2 :: tempList, result)
              else bubble(h2 :: t, h1 :: tempList, result)
          case h1 :: t => sort(tempList, h1 :: result)
    }
    sort(inputList, Nil)
   }

3
投票

找出一些Scala代码的最佳方法是在REPL中运行它:

scala> 5 to 0 by -1
res0: scala.collection.immutable.Range = Range(5, 4, 3, 2, 1, 0)

所以代码从(i-1)计数到0,倒退。

更一般地说,x to y创建一个从整数x到整数y的范围。 by部分修改了这个计数。例如,0 to 6 by 2表示“从0到6乘2”,或Range(0, 2, 4, 6)。在我们的例子中,by -1表示我们应该倒数1。

至于理解冒泡排序的工作原理,你应该阅读维基百科article并使用它来帮助你理解代码的作用。


0
投票

这可以是Bubble排序的最短功能实现

/**
 * Functional implementation of bubble sort
 * sort function swaps each element in the given list and create new list and iterate the same operation for length of the list times.
 * sort function takes three parameters
 * a) iteration list -> this is used to track the iteration. after each iteration element is dropped so that sort function exists the iteration list is empty
 * b) source list -> this is source list taken for element wise sorting
 * c) result -> stores the element as it get sorted and at end of each iteration, it will be the source for next sort iteration
 */

object Test extends App  {
  def bubblesort(source: List[Int]) : List[Int]  = {
    @tailrec
    def sort(iteration: List[Int], source: List[Int] , result: List[Int]) : List[Int]= source match {
      case h1 :: h2 :: rest => if(h1 > h2) sort(iteration, h1 :: rest, result :+ h2) else sort(iteration, h2 :: rest, result :+ h1) 
      case l:: Nil => sort(iteration, Nil, result :+ l)
      case Nil => if(iteration.isEmpty) return result else sort(iteration.dropRight(1), result, Nil )
    }
    sort(source,source,Nil)
  }
  println(bubblesort(List(4,3,2,224,15,17,9,4,225,1,7)))
 //List(1, 2, 3, 4, 4, 7, 9, 15, 17, 224, 225)
}
© www.soinside.com 2019 - 2024. All rights reserved.