Skip to content

Instantly share code, notes, and snippets.

@Porges
Created January 30, 2019 20:50
Show Gist options
  • Save Porges/c2e4a548af18b5ec684dcdd0e439d610 to your computer and use it in GitHub Desktop.
Save Porges/c2e4a548af18b5ec684dcdd0e439d610 to your computer and use it in GitHub Desktop.
void QuickSort<T>(FutureList<T> list)
where T: IComparable<T>
{
QuickSort(list, list.FirstIndex, list.LastIndex);
}
void QuickSort<T>(FutureList<T> list, Index<T> lo, Index<T> hi)
where T : IComparable<T>
{
if (lo < hi)
{
var p = Partition(list, lo, hi);
QuickSort(list, lo, p - 1);
QuickSort(list, p + 1, hi);
}
}
Index<T> Partition<T>(FutureList<T> list, Index<T> lo, Index<T> hi)
where T : IComparable<T>
{
var pivot = list[hi];
var i = lo;
for (var j = lo; j < hi; j = j + 1)
{
if (list[j].CompareTo(pivot) < 0)
{
list.Swap(i, j);
i = i + 1;
}
}
list.Swap(i, hi);
return i;
}
struct Count<T>
{
public static implicit operator Count<T>(int count) => throw new NotImplementedException(); // for now...
}
struct Index<T>
{
public static bool operator <(Index<T> l, Index<T> r) => throw new NotImplementedException();
public static bool operator >(Index<T> l, Index<T> r) => throw new NotImplementedException();
public static Index<T> operator +(Index<T> l, Count<T> other) => throw new NotImplementedException();
public static Index<T> operator -(Index<T> l, Count<T> other) => throw new NotImplementedException();
}
class FutureList<T>
{
public ref T this[Index<T> ix] => throw new NotImplementedException();
public Index<T> FirstIndex => throw new NotImplementedException();
public Index<T> LastIndex => throw new NotImplementedException();
}
static class FutureListExtensions
{
public static void Swap<T>(this FutureList<T> list, Index<T> i, Index<T> j)
{
var tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment