score:0
here's a fixed version that really does a bubblesort. i have no idea how you expected the other version to work, so this just does the obvious "compares each pair of adjacent items"
implicit class arraywrappedswap[a](array: array[a]) {
def swap(current: int, target: int): unit = {
val tmp = array(target)
array(target) = array(current)
array(current) = tmp
}
}
def bubblesort[a](arr: array[a])(implicit ordering: ordering[a]): array[a] = {
var continuesort = true
arr.indices.foreach(time => {
if (!continuesort) {
return arr
} else {
continuesort = false
(time until arr.length-1).foreach { index =>
if (ordering.compare(arr(index), arr(index + 1)) > 0) {
arr.swap(index, index+1)
continuesort = true
}
}
}
})
arr
} //> bubblesort: [a](arr: array[a])(implicit ordering: ordering[a])array[a]
println(bubblesort(array(1, 3, 1, 2, 3)).mkstring(","))
Source: stackoverflow.com
Related Query
- Why the scala :_* to expand a Seq into variable-length argument list does not work in this case?
- Why does this Scala placeholder not work
- Why does this Scala bubbleSorting not work?
- Why does pattern matching in Scala not work with variables?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why scala does not unify this type lambda with underlying type?
- Why this structural type bound does not work as expected?
- Why does the same scala code work OK in command line while not in Intellij?
- Why this simple Scala for comprehension does not execute the futures?
- Scala - Why does dotless not apply to this case
- Why does Scala implicit conversion work here with two args but not with one?
- Why does this json4s code work in the scala repl but fail to compile?
- Why does Regex pattern matching not work sometimes in Scala
- Why does Scala reify not work as according to the docs?
- Why does string interpolation not work in this case?
- Why does this piece of Scala code not compile?
- why split does not work in yield comprehension scala
- Why does this Scala code not compile?
- Why this foldLeft scala code does not work?
- Why Scala Enumeration does not work in Apache Zeppelin but it works in maven
- Why is this Scala Akka code does not compile?
- Why does Scala reflection work inside an object but not at the top level of a script?
- Any reason why scala does not explicitly support dependent types?
- Why does a small change to this Scala code make such a huge difference to performance?
- Eclipse, Android, Scala made easy but still does not work
- scala string.split does not work
- Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?
- Why does Scala not have a return/unit function defined for each monad (in contrast to Haskell)?
- Why does this compile under Java 7 but not under Java 8?
- Why does this explicit call of a Scala method allow it to be implicitly resolved?
More Query from same tag
- Nested environments in Scala REPL
- Scala, reflection of a concrete implementation of a parameterized trait
- Silhouette Unexpected exception playframework 2.6
- What is the time complexity of sliding and grouped functions of list in scala
- Pass a reference to a no-argument method in scala
- How to decode HTML entities in Spark-scala?
- Slick column mapper java.sql.Time to Joda LocalTime
- Scala nest data structure
- Implicit search does not guide type inference to find a supertype in Scala
- Spark-Scala: How to compare dates in strings of varying length and return minimum value?
- takeWhile in Scala for element n+1
- Spark/Scala : Creating Nested Structure using ReduceByKey using RDD only
- Scala JUnit assertSame
- Is there a way to omit processing over a RDD partition with few elements in Spark?
- How to convert case class RDD to RDD[String]?
- sbt integration tests returning exit code 0 when tests fail
- Shorthand arithmetic operators return different results in Scala - EG: 3 + 2 != 3.+(2)
- Spark print result of Arrays or saveAsTextFile
- Slick 3.1 - Printing SQL from DBIOAction (insert statements)
- Scala - How to avoid if/else condition for object factory
- object time is not a member of package org.joda
- Mapping Through 3 MongoD collections
- java.lang.ClassNotFoundException: org.jboss.netty.channel.ChannelFactory while running play project in intellij idea
- hdfs connection error from scala
- Adding Library to SBT Test File
- Partition functions in spark scala
- Querulous -- Is it usable with a database other than MySQL?
- Akka Http actor injection in trait
- Cant move to next line while reading csv file
- Spark w/ Scala way of using multiple heterogeneous columns in a UDF