Skip to content

Commit

Permalink
Merge pull request kennyledet#389 from aayushKumarJarvis/master
Browse files Browse the repository at this point in the history
Scala Implementation of QuickSort
  • Loading branch information
jcla1 committed Jul 25, 2014
2 parents e1b0974 + 2c01097 commit 238a8b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Quick_Sort/Scala/aayushKumarJarvis/QuickSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* SCALA Implementation of Quick Sort.
Though the corner cases are covered. But still if you find any additions to it,
please do add a test for it.
Any improvements/tests in the code is highly appreciated.
*/

class QuickSort {

def quickSort(listToBeSorted: List[Int]): List[Int] =
if (listToBeSorted.isEmpty) {
listToBeSorted
}
else {
val (smaller, bigger) = listToBeSorted.tail partition (_ < listToBeSorted.head)
quickSort(smaller) ::: listToBeSorted.head :: quickSort(bigger)
}
}
14 changes: 14 additions & 0 deletions Quick_Sort/Scala/aayushKumarJarvis/QuickSortTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import org.scalatest.{Matchers, FunSuite}

class QuickSortTest extends FunSuite with Matchers {

test("Quick Sort should sort") {
val objectForQuickSort = new QuickSort

objectForQuickSort.quickSort(List(2,1,55,21,58)) should be(List(1,2,21,55,58))
objectForQuickSort.quickSort(List(1,1,2,2,6,6,5,5)) should be(List(1,1,2,2,5,5,6,6))
objectForQuickSort.quickSort(List(0,22,1,45)) should be(List(0,1,22,45))
objectForQuickSort.quickSort(List(100,99,98,97,91)) should be(List(91,97,98,99,100))
}

}

0 comments on commit 238a8b6

Please sign in to comment.