From 537036f41b5074b070f99619e664afbed58bd55b Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 14 May 2012 00:32:10 -0400 Subject: [PATCH] First quicksort version although the tutorial did not have a class or object definition, which is necessary according to sbt ... hmmm. --- scala/official-by-example/.env | 2 ++ scala/official-by-example/.gitignore | 2 ++ scala/official-by-example/byx/build.sbt | 3 +++ .../byx/src/main/scala/sort.scala | 23 +++++++++++++++++++ scala/official-by-example/setup | 10 ++++++++ 5 files changed, 40 insertions(+) create mode 100644 scala/official-by-example/.env create mode 100644 scala/official-by-example/.gitignore create mode 100644 scala/official-by-example/byx/build.sbt create mode 100644 scala/official-by-example/byx/src/main/scala/sort.scala create mode 100755 scala/official-by-example/setup diff --git a/scala/official-by-example/.env b/scala/official-by-example/.env new file mode 100644 index 0000000..580223b --- /dev/null +++ b/scala/official-by-example/.env @@ -0,0 +1,2 @@ +HERE=$(dirname "$1") +(echo "$PATH" | grep "$HERE/bin" >/dev/null 2>&1) || export PATH="$HERE/bin:$PATH" diff --git a/scala/official-by-example/.gitignore b/scala/official-by-example/.gitignore new file mode 100644 index 0000000..f984205 --- /dev/null +++ b/scala/official-by-example/.gitignore @@ -0,0 +1,2 @@ +/bin/ +target/ diff --git a/scala/official-by-example/byx/build.sbt b/scala/official-by-example/byx/build.sbt new file mode 100644 index 0000000..d92a28b --- /dev/null +++ b/scala/official-by-example/byx/build.sbt @@ -0,0 +1,3 @@ +name := "byx" + +version := "0.1.0" diff --git a/scala/official-by-example/byx/src/main/scala/sort.scala b/scala/official-by-example/byx/src/main/scala/sort.scala new file mode 100644 index 0000000..2ce98cc --- /dev/null +++ b/scala/official-by-example/byx/src/main/scala/sort.scala @@ -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) + } +} diff --git a/scala/official-by-example/setup b/scala/official-by-example/setup new file mode 100755 index 0000000..4032ce5 --- /dev/null +++ b/scala/official-by-example/setup @@ -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