因此,为了快速选择实际可行的方法,在创建原始数组的3个必要分区时会遇到一些问题。自定义数据类型基本上只是手动创建矢量。
public T MinExtended(T[] data, int k)
{
Random rnd = new Random();
T pivot = data[rnd.Next(0, data.Length - 1)];
if (data.Length == 1)
return data[0];
T[] Less = new T[data.Length];
T[] Greater = new T[data.Length];
T[] Equal = new T[data.Length];
int less = 0;
int greater = 0;
int equal = 0;
for (int i = 0; i < data.Length - 1; i++)
{
if (data[i].CompareTo(pivot) == -1)
{
Less[less] = data[i];
less++;
}
if (data[i].CompareTo(pivot) == 1)
{
Greater[greater] = data[i];
greater++;
}
if (data[i].CompareTo(pivot) == 0)
{
Equal[equal] = data[i];
equal++;
}
}
if (Less.Length.CompareTo(k) >= 0)
return MinExtended(Less, k);
if ((Less.Length + Equal.Length).CompareTo(k) >= 0)
return pivot;
else
return MinExtended(Greater, k - Less.Length - Equal.Length);
}
这当然会返回
堆栈溢出错误。
我不确定为什么或如何解决它