Below is an implementation of a quicksort the java way
void sort(int[] xs) {
sort(xs, 0, xs.length -1 );
}
void sort(int[] xs, int l, int r) {
int pivot = xs[(l+r)/2];
int a = l; int b = r;
while (a <= b)
while (xs[a] < pivot) { a = a + 1; }
while (xs[b] > pivot) { b = b – 1; }
if (a <= b) {
swap(xs, a, b);
a = a + 1;
b = b – 1;
}
}
if (l < b) sort(xs, l, b);
if (b < r) sort(xs, a, r);
}
void swap(int[] arr, int i, int j) {
int t = arr[i]; arr[i] = arr[j]; arr[j] = t;
}
Quicksort the scala way ... looks neater and is easier to understand
object QuickSort {
def sort(xs:Array[Int]):Array[Int]={
if(xs.length<1)xs
else{
val pivot=xs(xs.length/2)
Array.concat(
sort(xs filter (pivot >)),
xs filter (pivot ==),
sort(xs filter (pivot <)))
}
}
}
Friday, May 2, 2008
Scala vs Java : Implementing quicksort -- not optimized
Blog Archive
-
▼
2008
(26)
-
▼
May
(10)
- new java puzzlers (As presented in Javapolis --2007)
- How to derail your experts and ruin their performance
- Living out of the box
- The Dreyfus model
- The bane of java exceptional handling
- Iterating over Map's
- N Queen Problem
- Identifying Patterns and creating Higher Order Fun...
- Tail Recursion
- Scala vs Java : Implementing quicksort -- not opti...
-
▼
May
(10)

The emperor and me beaching

The Devil next door

Kaiser The Emperor