From 8aced9e71f9facebbb99455513b454ae545be751 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 28 Dec 2018 11:25:45 +0800 Subject: [PATCH] impl sqrt and b search --- .../src/main/scala/ch15_bsearch/BSearch.scala | 60 ++++++++++++++----- .../scala/ch15_bsearch/BSearchRecursive.scala | 31 +++++----- .../ch15_bsearch/BSearchRecursiveTest.scala | 25 ++++++++ .../test/scala/ch15_bsearch/BSearchTest.scala | 37 ++++++++++++ 4 files changed, 123 insertions(+), 30 deletions(-) create mode 100644 scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala create mode 100644 scala/src/test/scala/ch15_bsearch/BSearchTest.scala diff --git a/scala/src/main/scala/ch15_bsearch/BSearch.scala b/scala/src/main/scala/ch15_bsearch/BSearch.scala index 2165938..9a9ef02 100644 --- a/scala/src/main/scala/ch15_bsearch/BSearch.scala +++ b/scala/src/main/scala/ch15_bsearch/BSearch.scala @@ -1,20 +1,50 @@ package ch15_bsearch -object BSearch { - def search(nums: Array[Int], target: Int): Int = { - var low = 0 - var high = nums.length - 1 - while(low <= high){ - val mid = low + ((high - low) >> 2) - if(nums(mid) > target){ - high = mid - 1 - } else if (nums(mid) < target){ - low = mid + 1 - } else { - return mid - } - } +import scala.math.abs - return -1 +object BSearch { + + def search(items: Array[Int], target: Int): Int = { + var low = 0 + var high = items.length - 1 + while (low <= high) { + val mid = low + (high - low) / 2 + if (items(mid) == target) { + return mid + } else if (items(mid) > target) { + high = mid - 1 + } else { + low = mid + 1 + } } + + -1 + } + + def sqrt(x: Double, precision: Double): Double = { + + require(precision > 0, "precision must > 0") + require(x > 0, "input value for sqrt must > 0") + var low = 0.0 + var high = x + val actualPrecision = precision / 10 + + if (x > 0 && x < 1) { + low = x + high = 1 + } + while (high - low > actualPrecision) { + val mid = low + (high - low) / 2 + if (abs(mid * mid - x) < actualPrecision) { + //find it + return mid + } else if (mid * mid > x) { + high = mid + } else { + low = mid + } + } + throw new IllegalStateException("could not determine the sqrt value for " + x) + + } } diff --git a/scala/src/main/scala/ch15_bsearch/BSearchRecursive.scala b/scala/src/main/scala/ch15_bsearch/BSearchRecursive.scala index ca7befc..8ea4010 100644 --- a/scala/src/main/scala/ch15_bsearch/BSearchRecursive.scala +++ b/scala/src/main/scala/ch15_bsearch/BSearchRecursive.scala @@ -1,22 +1,23 @@ package ch15_bsearch object BSearchRecursive { - def search(nums: Array[Int], target: Int): Int = { - return searchInternal(nums, target, 0, nums.length - 1) + + def search(items: Array[Int], target: Int): Int = { + _search(items, target, 0, items.length - 1) + } + + private[this] def _search(items: Array[Int], target: Int, low: Int, high: Int): Int = { + if (low > high) { + return -1 } - def searchInternal(nums:Array[Int], target: Int, low: Int, high: Int): Int = { - if(low <= high){ - val mid = low + ((high - low) >> 2) - if(nums(mid) > target){ - searchInternal(nums, target, low, mid - 1) - } else if (nums(mid) < target){ - searchInternal(nums, target, mid + 1, high) - } else { - return mid - } - }else{ - return -1 - } + val mid = low + (high - low) / 2 + if (items(mid) == target) { + mid + } else if (items(mid) > target) { + _search(items, target, low, mid - 1) + } else { + _search(items, target, mid + 1, high) } + } } diff --git a/scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala b/scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala new file mode 100644 index 0000000..0aa1702 --- /dev/null +++ b/scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala @@ -0,0 +1,25 @@ +package ch15_bsearch + +import ch12_sorts.QuickSort +import org.scalatest.{FlatSpec, Matchers} + +import scala.util.Random + +class BSearchRecursiveTest extends FlatSpec with Matchers { + + behavior of "BSearchRecursiveTest" + + it should "search with exist value" in { + val length = 50000 + val array = new Array[Int](length) + val rnd = new Random() + for (i <- Range(0, length)) { + array(i) = rnd.nextInt() + } + + val target = array(2698) + + BSearchRecursive.search(QuickSort.quickSort(array), target) should be > -1 + } + +} diff --git a/scala/src/test/scala/ch15_bsearch/BSearchTest.scala b/scala/src/test/scala/ch15_bsearch/BSearchTest.scala new file mode 100644 index 0000000..437e96a --- /dev/null +++ b/scala/src/test/scala/ch15_bsearch/BSearchTest.scala @@ -0,0 +1,37 @@ +package ch15_bsearch + +import ch12_sorts.QuickSort +import org.scalatest.{FlatSpec, Matchers} + +import scala.util.Random + +class BSearchTest extends FlatSpec with Matchers { + + behavior of "BSearchTest" + + it should "search with exist value" in { + val length = 50000 + val array = new Array[Int](length) + val rnd = new Random() + for (i <- Range(0, length)) { + array(i) = rnd.nextInt() + } + + val target = array(2698) + + BSearch.search(QuickSort.quickSort(array), target) should be > -1 + } + + it should "calculate sqrt value -1 " in { + val x = 4 + val precision = 0.000001 + BSearch.sqrt(x, precision) should equal(2.0) + } + + it should "calculate sqrt value -2 " in { + val x = 0.04 + val precision = 0.000001 + BSearch.sqrt(x, precision) should equal(0.2 +- precision) + } + +}