From 806339423662c86567f0a1985629f6909284aa68 Mon Sep 17 00:00:00 2001 From: yangchuz Date: Tue, 23 Oct 2018 09:34:43 +0800 Subject: [PATCH 1/5] Create Cha11Sorts.scala --- scala/Cha11Sorts.scala | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 scala/Cha11Sorts.scala diff --git a/scala/Cha11Sorts.scala b/scala/Cha11Sorts.scala new file mode 100644 index 0000000..af452e1 --- /dev/null +++ b/scala/Cha11Sorts.scala @@ -0,0 +1,28 @@ +import scala.util.control.Breaks._ + +object Cha11Sorts { + def main(args: Array[String]): Unit ={ + println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", ")) + } + + def bubbleSort(arr: Array[Int]): Array[Int] = { + val l = arr.length + breakable { + for(i <- (l-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 + } + } + if(!flag){ + break + } + } + } + arr + } +} From 3675bbc23597a223f4ac42c8291cc56ee8e4fcbe Mon Sep 17 00:00:00 2001 From: yangchuz Date: Tue, 23 Oct 2018 09:36:17 +0800 Subject: [PATCH 2/5] Create Sorts.scala --- scala/11_sorts/Sorts.scala | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 scala/11_sorts/Sorts.scala diff --git a/scala/11_sorts/Sorts.scala b/scala/11_sorts/Sorts.scala new file mode 100644 index 0000000..04accbf --- /dev/null +++ b/scala/11_sorts/Sorts.scala @@ -0,0 +1,27 @@ +import scala.util.control.Breaks._ +object Sorts { + def main(args: Array[String]): Unit ={ + println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", ")) + } + + def bubbleSort(arr: Array[Int]): Array[Int] = { + val l = arr.length + breakable { + for(i <- (l-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 + } + } + if(!flag){ + break + } + } + } + arr + } +} From 182dee6e921fe382b7a047cbb67e2a5bbd424955 Mon Sep 17 00:00:00 2001 From: yangchuz Date: Tue, 23 Oct 2018 09:36:33 +0800 Subject: [PATCH 3/5] Delete Cha11Sorts.scala --- scala/Cha11Sorts.scala | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 scala/Cha11Sorts.scala diff --git a/scala/Cha11Sorts.scala b/scala/Cha11Sorts.scala deleted file mode 100644 index af452e1..0000000 --- a/scala/Cha11Sorts.scala +++ /dev/null @@ -1,28 +0,0 @@ -import scala.util.control.Breaks._ - -object Cha11Sorts { - def main(args: Array[String]): Unit ={ - println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", ")) - } - - def bubbleSort(arr: Array[Int]): Array[Int] = { - val l = arr.length - breakable { - for(i <- (l-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 - } - } - if(!flag){ - break - } - } - } - arr - } -} From b06c99632b7290783c1074e6fa6fa6c57de7cceb Mon Sep 17 00:00:00 2001 From: yangchuz Date: Tue, 23 Oct 2018 09:54:51 +0800 Subject: [PATCH 4/5] Update Sorts.scala --- scala/11_sorts/Sorts.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scala/11_sorts/Sorts.scala b/scala/11_sorts/Sorts.scala index 04accbf..81124ef 100644 --- a/scala/11_sorts/Sorts.scala +++ b/scala/11_sorts/Sorts.scala @@ -1,7 +1,7 @@ import scala.util.control.Breaks._ object Sorts { def main(args: Array[String]): Unit ={ - println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", ")) + println(bubbleSort(Array(0, 3, 7, 6, 4, 5, 1, 2)).mkString(", ")) } def bubbleSort(arr: Array[Int]): Array[Int] = { @@ -10,7 +10,7 @@ object Sorts { for(i <- (l-1) to (1, -1)){ var flag = false for(j <- 0 until i){ - if(arr(j) < arr(j+1)){ + if(arr(j) > arr(j+1)){ val tmp = arr(j) arr(j) = arr(j+1) arr(j+1) = tmp From 66c64c532c4cef162e9800dd8ec8f5d66297db57 Mon Sep 17 00:00:00 2001 From: yangchuz Date: Tue, 23 Oct 2018 17:52:09 +0800 Subject: [PATCH 5/5] Scala implementation for bubble sort, insertion sort and selection sort. --- scala/11_sorts/Sorts.scala | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/scala/11_sorts/Sorts.scala b/scala/11_sorts/Sorts.scala index 81124ef..dfce2d7 100644 --- a/scala/11_sorts/Sorts.scala +++ b/scala/11_sorts/Sorts.scala @@ -1,13 +1,21 @@ import scala.util.control.Breaks._ + +/** + * 冒泡排序、插入排序、选择排序 + * + * Author: yangchuz + */ object Sorts { def main(args: Array[String]): Unit ={ - println(bubbleSort(Array(0, 3, 7, 6, 4, 5, 1, 2)).mkString(", ")) +// 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]): Array[Int] = { - val l = arr.length + def bubbleSort(arr: Array[Int], n:Int): Array[Int] = { + val n = arr.length breakable { - for(i <- (l-1) to (1, -1)){ + for(i <- (n-1) to (1, -1)){ var flag = false for(j <- 0 until i){ if(arr(j) > arr(j+1)){ @@ -24,4 +32,37 @@ object Sorts { } arr } + + 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 + break + } + } + } + } + arr + } + + 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 + } + } + + val tmp = arr(i) + arr(i) = arr(min) + arr(min) = tmp + } + arr + } }