immutable implementation for quicksort (no swapping)