From d6c77c8b552eaf30864b1e93b0494217ba43a8e8 Mon Sep 17 00:00:00 2001 From: NeverGiveUPek Date: Mon, 10 Jan 2022 14:25:58 +0100 Subject: [PATCH] Add list 7 files --- Fibonacci.scala | 44 +++++++++++++++++++++ L7.scala | 95 ++++++++++++++++++++++++++++++++++++++++++++++ ListComparer.scala | 50 ++++++++++++++++++++++++ QuickSort.scala | 28 ++++++++++++++ 4 files changed, 217 insertions(+) create mode 100644 Fibonacci.scala create mode 100644 L7.scala create mode 100644 ListComparer.scala create mode 100644 QuickSort.scala diff --git a/Fibonacci.scala b/Fibonacci.scala new file mode 100644 index 00000000..01e05215 --- /dev/null +++ b/Fibonacci.scala @@ -0,0 +1,44 @@ +import scala.concurrent.{Await, Future} +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration.Duration + +object Fibonacci { + + def fibonacci(n :Int): Int = + n match + case 1 => 1 + case 2 => 1 + case _ => fibonacci(n-1) + fibonacci(n-2) + + + def fibonacciParallel(n :Int, max: Int): Int = + fibonacciParallelSplit(n, 0, max) + + + + def fibonacciParallelSplit(n :Int, current: Int, max: Int): Int = + if(current < max - 1) then + n match + case 1 => 1 + case 2 => 1 + case _ => + //split + val fib1 = Future(fibonacciParallelSplit(n-1,current + 2, max) ) + val fib2 = Future(fibonacciParallelSplit(n-2,current + 2,max) ) + + //wait for results + val result1 = Await.result(fib1, Duration.Inf) + val result2 = Await.result(fib2, Duration.Inf) + + //return + result1 + result2 + else + n match + case 1 => 1 + case 2 => 1 + case _ => fibonacci(n-1) + fibonacci(n-2) + + + + +} diff --git a/L7.scala b/L7.scala new file mode 100644 index 00000000..d7148837 --- /dev/null +++ b/L7.scala @@ -0,0 +1,95 @@ +import Fibonacci.* +import ListComparer.* +import QuickSort.* + +import java.security.cert.PolicyQualifierInfo +import scala.concurrent.{Await, Future} +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.duration.Duration + +def countTimeNs[T](task: => T): Long = { + val start = System.nanoTime(); + task + val end = System.nanoTime() + + (end - start) +} + +def printTime(time: Long): Unit = { + val nano = time + val mili = time / 1000000 + + println(s"Mineło: ${nano} nanosekund, ${mili} milisekund") + println() +} + + +object L7 { + def main(args: Array[String]): Unit = { + + + + + + print("Fibonacci: ") + printTime(countTimeNs(fibonacci(10))) + print("FibonacciParallel (6): ") + printTime(countTimeNs(fibonacciParallel(10,6))) + print("FibonacciParallel (18): ") + printTime(countTimeNs(fibonacciParallel(10,18))) + print("FibonacciParallel (2): ") + printTime(countTimeNs(fibonacciParallel(10,2))) + + + print("Fibonacci: ") + printTime(countTimeNs(fibonacci(45))) + print("FibonacciParallel (6): ") + printTime(countTimeNs(fibonacciParallel(45,6))) + print("FibonacciParallel (18): ") + printTime(countTimeNs(fibonacciParallel(45,18))) + print("FibonacciParallel (2): ") + printTime(countTimeNs(fibonacciParallel(45,2))) + + + println() + println() + println() + + val listSmall1 = createList(1000,100_000) + val listSmall2 = createList(1000,100_000) + + + val listBig1 = createList(10,50_000_000) + val listBig2 = createList(10,50_000_000) + + print("isSumGreater: ") + printTime(countTimeNs(isSumGreater(listSmall1,listSmall2))) + print("isSumGreaterParallel: ") + printTime(countTimeNs(isSumGreaterParallel(listSmall1,listSmall2))) + + print("isSumGreater: ") + printTime(countTimeNs(isSumGreater(listBig1,listBig2))) + print("isSumGreaterParallel: ") + printTime(countTimeNs(isSumGreaterParallel(listBig1,listBig2))) + + println() + println() + println() + + val listSmall = List(1,2,3,-1,3,12,41,1,24,1,4,1,41,6,27,345,32) + val listBig = createList(1000, 50) + + print("quicksort: ") + printTime(countTimeNs(quickSort(listSmall))) + print("quicksortParallel: ") + printTime(countTimeNs(quickSortParallel(listSmall))) + + + print("quicksort: ") + printTime(countTimeNs(quickSort(listBig))) + print("quicksortParallel: ") + printTime(countTimeNs(quickSortParallel(listBig))) + + + } +} diff --git a/ListComparer.scala b/ListComparer.scala new file mode 100644 index 00000000..0be2a700 --- /dev/null +++ b/ListComparer.scala @@ -0,0 +1,50 @@ +import scala.concurrent.duration.Duration +import scala.concurrent.{Await, Future} + +object ListComparer { + + def createList(maxNumber: Int, amount: Int):List[Int] = { + val random = scala.util.Random + val seq = for(i <- 0 to amount) yield random.nextInt(maxNumber) + seq.toList + } + + def sum(list: List[Int]):Long = + def sumHelper(rest: List[Int], sum: Int):Long = + rest match + case Nil => sum + case _ => sumHelper(rest.tail, sum + rest.head) + + sumHelper(list,0) + + + + + + def isSumGreater(first: List[Int], second: List[Int]):Boolean = { + val firstSum = sum(first) + val secondSum = sum(second) + + //println(s"${firstSum} ${secondSum}") + + + firstSum > secondSum + } + + + def isSumGreaterParallel(first: List[Int], second: List[Int]):Boolean = { + implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global + + val sumFirstFuture = Future(sum(first)) + val sumSecondFuture = Future(sum(second)) + + val resultFirst = Await.result(sumFirstFuture, Duration.Inf) + val resultSecond = Await.result(sumSecondFuture, Duration.Inf) + + //println(s"${resultFirst} ${resultSecond}") + + + resultFirst > resultSecond + + } +} diff --git a/QuickSort.scala b/QuickSort.scala new file mode 100644 index 00000000..2fa734d9 --- /dev/null +++ b/QuickSort.scala @@ -0,0 +1,28 @@ +import scala.concurrent.duration.Duration +import scala.concurrent.{Await, Future} + +object QuickSort { + implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global + + def quickSort(list: List[Int]): List[Int] = + list match + case Nil => list + case el :: Nil => list + case pivot :: _ => + val tail = list.tail + quickSort(tail.filter(i => i < pivot)) ::: list.filter(i => i == pivot) ::: quickSort(quickSort(tail.filter(i => i > pivot))) + + + def quickSortParallel(list: List[Int]): List[Int] = + list match + case Nil => list + case el :: Nil => list + case pivot :: _ => + val tail = list.tail + val smallFuture = Future(quickSort(tail.filter(i => i < pivot))) + val bigFuture = Future(quickSort(tail.filter(i => i > pivot))) + + + Await.result(smallFuture, Duration.Inf) ::: list.filter(i => i == pivot) ::: Await.result(bigFuture, Duration.Inf) + +}