Hi fellow coders, I spent a couple of hours to implement a working, basic, quicksort. Practicing D. It finally boils down to that (intent to use the std, not to rewrite swap, etc):

--------------------------------
import std.stdio : writeln;
import std.algorithm.sorting;

pure void quickSort(T) (T[] r)
{
  if (r.length > 1)
  {
size_t p = pivotPartition(r, r.length-1); //r[$-1] is swapped to r[p]

    quickSort( r[ 0..p ] );
    quickSort( r[ p+1..$ ] );
  }
}

void main()
{
  int[] arr = [9,7, 4 , 8, 5, 3, 1];
  quickSort!(int)(arr);
  writeln("arr : ", arr );

}
--------------------------------

I spent some time understanding "ranges", but at the end I am surprised I didn't use them. At the beginning I wrote something like quickSort( Range r ) and tried randomaccessrange etc but I didn't manage to make it work.

My question is, is there a way to write this with "ranges" as the argument, or does this concept of slices with the [] notation takes precedence somehow on the concept of ranges, in this particular case?

2) how would YOU write it? Using std or not.

3) any comment/code correction welcome, I am learning :)

cheers
J

Reply via email to