forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request wangzheng0822#215 from email2liyang/master
scala all sorts implementation
- Loading branch information
Showing
16 changed files
with
823 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ch12_sorts | ||
|
||
object MergeSort { | ||
|
||
def mergeSort(items: Array[Int]): Array[Int] = { | ||
_mergeSort(items, 0, items.length - 1) | ||
items | ||
} | ||
|
||
|
||
private[this] def _mergeSort(items: Array[Int], p: Int, r: Int): Unit = { | ||
if (p >= r) { | ||
return | ||
} | ||
|
||
val q = p + (r - p) / 2 | ||
_mergeSort(items, p, q) | ||
_mergeSort(items, q + 1, r) | ||
_merge(items, p, q, r) | ||
|
||
} | ||
|
||
private[this] def _merge(items: Array[Int], p: Int, q: Int, r: Int): Unit = { | ||
//start of first half | ||
var i = p | ||
//start of second half | ||
var j = q + 1 | ||
var k = 0 | ||
//temp array to hold the data | ||
val tempArray = new Array[Int](r - p + 1) | ||
while (i <= q && j <= r) { | ||
if (items(i) <= items(j)) { | ||
tempArray(k) = items(i) | ||
i += 1 | ||
} else { | ||
tempArray(k) = items(j) | ||
j += 1 | ||
} | ||
k += 1 | ||
} | ||
|
||
var start = i | ||
var end = q | ||
|
||
if (j <= r) { | ||
start = j | ||
end = r | ||
} | ||
|
||
for (n <- start to end) { | ||
tempArray(k) = items(n) | ||
k += 1 | ||
} | ||
|
||
//copy tempArray back to items | ||
for (n <- 0 to r - p) { | ||
items(p + n) = tempArray(n) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ch12_sorts | ||
|
||
object QuickSort { | ||
|
||
//find the K th smallest element int the array | ||
def findKthElement(items: Array[Int], k: Int): Int = { | ||
_findKthElement(items, k, 0, items.length - 1) | ||
} | ||
|
||
private[this] def _findKthElement(items: Array[Int], k: Int, p: Int, r: Int): Int = { | ||
val q = _partition(items, p, r) | ||
|
||
if (k == q + 1) { | ||
items(q) | ||
} else if (k < q + 1) { | ||
_findKthElement(items, k, p, q - 1) | ||
} else { | ||
_findKthElement(items, k, q + 1, r) | ||
} | ||
} | ||
|
||
def quickSort(items: Array[Int]): Array[Int] = { | ||
_quickSort(items, 0, items.length - 1) | ||
items | ||
} | ||
|
||
private[this] def _quickSort(items: Array[Int], p: Int, r: Int): Unit = { | ||
if (p >= r) { | ||
return | ||
} | ||
val q = _partition(items, p, r) | ||
_quickSort(items, p, q - 1) | ||
_quickSort(items, q + 1, r) | ||
} | ||
|
||
private[this] def _partition(items: Array[Int], p: Int, r: Int): Int = { | ||
val pivot = items(r) | ||
var i = p | ||
for (j <- Range(p, r)) { | ||
if (items(j) < pivot) { | ||
val temp = items(i) | ||
items(i) = items(j) | ||
items(j) = temp | ||
i += 1 | ||
} | ||
} | ||
|
||
val temp = items(i) | ||
items(i) = items(r) | ||
items(r) = temp | ||
|
||
i | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,50 @@ | ||
package ch15_bsearch | ||
|
||
import scala.math.abs | ||
|
||
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 | ||
} | ||
} | ||
|
||
return -1 | ||
|
||
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) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
Oops, something went wrong.