Scala / Scala programs
1.
Implement Selection sort program using Scala.
2.
Implement bubble sort using Scala.
3.
I have a string(avbfjsjebfswncnsjfsfna) and I don't want repeated alphabets by using pure Scala programming.
Could not find what you were looking for? send
us the question and we would be happy to answer your question.
1. Implement Selection sort program using Scala.
object SelectionSort { def main (args: Array[String]) { val myArray = Array(14,2,5,1,2,6) for ( i <- 0 to myArray.length-2) { var min =myArray(i) var pos = i for ( j <- i to myArray.length-1) if (min> myArray(j)) { min = myArray(j) pos = j } if (myArray(i) != min) { val temp =myArray(pos) myArray(pos) =myArray(i) myArray(i) = temp } } println(myArray deep) } }
2. Implement bubble sort using Scala.
object BubbleSort { def main(args: Array[String]) { val myArray = Array(18, 2, 5, 1, 63, 17) var unsorted_last_index = myArray.length - 2; while (unsorted_last_index > 0) { for (j <- 0 to unsorted_last_index) if (myArray(j) > myArray(j + 1)) { val temp = myArray(j) myArray(j) = myArray(j + 1) myArray(j + 1) = temp } unsorted_last_index = unsorted_last_index - 1; } println(myArray deep) } }
3. I have a string(avbfjsjebfswncnsjfsfna) and I don't want repeated alphabets by using pure Scala programming.
The Scala program follows. Kindly share your comment/suggestion for improvement and feedback. Thanks.
import scala.collection.mutable.HashSet object StringUtil { /* * The below program creates a new string object without duplicates from the original string. * please note that this programs consider character case (case sensitive) */ def main(args: Array[String]) { var myString:String = "avbfjsjebfswncnsjfsfnaA"; println ("The original String= " + myString); var charCount: HashSet[Char] = HashSet.empty[Char] var myoutputString:String = ""; for (c <-myString) { if (!charCount.contains(c)) {charCount +=c myoutputString +=c } } println ("The output String without duplicates= "+ myoutputString); } }