Skip to content

Commit

Permalink
Merge pull request wangzheng0822#215 from email2liyang/master
Browse files Browse the repository at this point in the history
scala all sorts implementation
  • Loading branch information
wangzheng0822 authored Jan 7, 2019
2 parents 405dc91 + b639322 commit 715c4e2
Show file tree
Hide file tree
Showing 16 changed files with 823 additions and 67 deletions.
74 changes: 37 additions & 37 deletions scala/src/main/scala/ch11_sorts/Sorts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,63 @@ import scala.util.control.Breaks.{break, breakable}
* Author: yangchuz
*/
object Sorts {
def main(args: Array[String]): Unit ={
// println(bubbleSort(Array(0, 6, 2, 3, 8, 5, 6, 7), 8).mkString(", "))
// println(insertSort(Array(0, 6, 2, 3, 8, 5, 6, 7), 8).mkString(", "))
println(selectionSort(Array(0, 6, 2, 3, 8, 5, 6, 7), 8).mkString(", "))
}

def bubbleSort(arr: Array[Int], n:Int): Array[Int] = {
val n = arr.length
def bubbleSort(items: Array[Int]): Array[Int] = {
val length = items.length
breakable {
for(i <- (n-1) to (1, -1)){
var flag = false
for(j <- 0 until i){
if(arr(j) > arr(j+1)){
val tmp = arr(j)
arr(j) = arr(j+1)
arr(j+1) = tmp
flag = true
for (i <- Range(0, length)) {
var exit = true
for (j <- Range(0, length - i - 1)) {
if (items(j + 1) < items(j)) {
val temp = items(j + 1)
items(j + 1) = items(j)
items(j) = temp
exit = false
}
}
if(!flag){
if (exit) {
break
}
}
}
arr
items
}

def insertSort(arr: Array[Int], n:Int): Array[Int] = {
for(i <- 1 until n){
val tmp = arr(i)
breakable{
for(j <- (i-1) to (0, -1)){
if(tmp < arr(j)){
arr(j+1) = arr(j)
}else{
arr(j+1) = tmp
def insertSort(items: Array[Int]): Array[Int] = {
val length = items.length
for (i <- Range(1, length)) {
val value = items(i)
var j = i - 1
breakable {
while (j >= 0) {
if (items(j) > value) {
items(j + 1) = items(j)
} else {
break
}
j -= 1
}
}
items(j + 1) = value
}
arr
items
}

def selectionSort(arr: Array[Int], n:Int): Array[Int] = {
for(i <- 0 until n){
var min = i
for(j <- (i + 1) until n){
if(arr(j) < arr(min)){
min = j
def selectionSort(items: Array[Int]): Array[Int] = {
val length = items.length
for (i <- Range(0, length)) {
var minIndex = i
for (j <- Range(i + 1, length)) {
if (items(j) < items(minIndex)) {
minIndex = j
}
}

val tmp = arr(i)
arr(i) = arr(min)
arr(min) = tmp
//put the min value to the front
val temp = items(i)
items(i) = items(minIndex)
items(minIndex) = temp
}
arr
items
}
}
60 changes: 60 additions & 0 deletions scala/src/main/scala/ch12_sorts/MergeSort.scala
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)
}
}
}
54 changes: 54 additions & 0 deletions scala/src/main/scala/ch12_sorts/QuickSort.scala
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
}
}
60 changes: 45 additions & 15 deletions scala/src/main/scala/ch15_bsearch/BSearch.scala
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)

}
}
31 changes: 16 additions & 15 deletions scala/src/main/scala/ch15_bsearch/BSearchRecursive.scala
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)
}
}
}
Loading

0 comments on commit 715c4e2

Please sign in to comment.