summaryrefslogtreecommitdiff
path: root/docs/examples/sort.scala
diff options
context:
space:
mode:
authorGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
committerGilles Dubochet <gilles.dubochet@epfl.ch>2005-12-16 18:44:33 +0000
commit53a3cc7b17f4cf97075b7e71720777fd84109696 (patch)
tree0cc784e0b47ea49cc151a136d19f20bfa8ee2197 /docs/examples/sort.scala
parentdf50e05006b43b007c2587549030d24b5c154398 (diff)
downloadscala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.gz
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.tar.bz2
scala-53a3cc7b17f4cf97075b7e71720777fd84109696.zip
Created proper 'docs' folder for new layout.
Diffstat (limited to 'docs/examples/sort.scala')
-rw-r--r--docs/examples/sort.scala48
1 files changed, 48 insertions, 0 deletions
diff --git a/docs/examples/sort.scala b/docs/examples/sort.scala
new file mode 100644
index 0000000000..6554a2415b
--- /dev/null
+++ b/docs/examples/sort.scala
@@ -0,0 +1,48 @@
+package examples;
+
+object sort {
+
+ def sort(a: Array[Int]): Unit = {
+
+ def swap(i: Int, j: Int): Unit = {
+ val t = a(i); a(i) = a(j); a(j) = t;
+ }
+
+ def sort1(l: Int, r: Int): Unit = {
+ val pivot = a((l + r) / 2);
+ var i = l;
+ var j = r;
+ while (i <= j) {
+ while (a(i) < pivot) { i = i + 1 }
+ while (a(j) > pivot) { j = j - 1 }
+ if (i <= j) {
+ swap(i, j);
+ i = i + 1;
+ j = j - 1;
+ }
+ }
+ if (l < j) sort1(l, j);
+ if (j < r) sort1(i, r);
+ }
+
+ if (a.length > 0)
+ sort1(0, a.length - 1);
+ }
+
+ def println(ar: Array[Int]) = {
+ def print1 = {
+ def iter(i: Int): String =
+ ar(i) + (if (i < ar.length-1) "," + iter(i+1) else "");
+ if (ar.length == 0) "" else iter(0)
+ }
+ Console.println("[" + print1 + "]")
+ }
+
+ def main(args: Array[String]) = {
+ val ar = Array(6, 2, 8, 5, 1);
+ println(ar);
+ sort(ar);
+ println(ar)
+ }
+
+}