impl sqrt and b search
This commit is contained in:
parent
df6133619d
commit
8aced9e71f
@ -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)
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala
Normal file
25
scala/src/test/scala/ch15_bsearch/BSearchRecursiveTest.scala
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
37
scala/src/test/scala/ch15_bsearch/BSearchTest.scala
Normal file
37
scala/src/test/scala/ch15_bsearch/BSearchTest.scala
Normal file
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user