First quicksort version
although the tutorial did not have a class or object definition, which is necessary according to sbt ... hmmm.
This commit is contained in:
parent
3075bdb938
commit
537036f41b
2
scala/official-by-example/.env
Normal file
2
scala/official-by-example/.env
Normal file
@ -0,0 +1,2 @@
|
||||
HERE=$(dirname "$1")
|
||||
(echo "$PATH" | grep "$HERE/bin" >/dev/null 2>&1) || export PATH="$HERE/bin:$PATH"
|
2
scala/official-by-example/.gitignore
vendored
Normal file
2
scala/official-by-example/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/bin/
|
||||
target/
|
3
scala/official-by-example/byx/build.sbt
Normal file
3
scala/official-by-example/byx/build.sbt
Normal file
@ -0,0 +1,3 @@
|
||||
name := "byx"
|
||||
|
||||
version := "0.1.0"
|
23
scala/official-by-example/byx/src/main/scala/sort.scala
Normal file
23
scala/official-by-example/byx/src/main/scala/sort.scala
Normal file
@ -0,0 +1,23 @@
|
||||
object sort {
|
||||
def sort(xs: Array[Int]) {
|
||||
def swap(i: Int, j: Int) {
|
||||
val t = xs(i); xs(i) = xs(j); xs(j) = t
|
||||
}
|
||||
def sort1(l: Int, r: Int) {
|
||||
val pivot = xs((l + r) / 2)
|
||||
var i = l; var j = r
|
||||
while (i <= j) {
|
||||
while (xs(i) < pivot) i += 1
|
||||
while (xs(j) > pivot) j -= 1
|
||||
if (i <= j) {
|
||||
swap(i, j)
|
||||
i += 1
|
||||
j -= 1
|
||||
}
|
||||
}
|
||||
if (l < j) sort1(l, j)
|
||||
if (j < r) sort1(i, r)
|
||||
}
|
||||
sort1(0, xs.length - 1)
|
||||
}
|
||||
}
|
10
scala/official-by-example/setup
Executable file
10
scala/official-by-example/setup
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
mkdir -p ./bin
|
||||
pushd ./bin
|
||||
curl -O 'https://raw.github.com/paulp/sbt-extras/master/sbt'
|
||||
chmod +x sbt
|
||||
popd
|
||||
./bin/sbt -sbt-create about
|
Loading…
Reference in New Issue
Block a user