public static T QuickSelect <T>(this IEnumerable <T> source, int begin, int end, int index, SelectType type, Func <T, T, bool> comparer) { List <T> list = source.ToList(); return(QuickSelectList(list, begin, end, index, type, comparer)); }
private static T QuickSelectList <T>(List <T> source, int begin, int end, int index, SelectType type, Func <T, T, bool> comparer) { if (end - begin + 1 >= index) { int partition = 0; if (type == SelectType.HEADER) { partition = CommonSort.Partition(source, begin, end, begin, comparer); } else if (type == SelectType.END) { partition = CommonSort.Partition(source, begin, end, end, comparer); } else { partition = CommonSort.Partition(source, begin, end, (begin + end) / 2, comparer); } if (index + begin < partition) { QuickSelectList(source, begin, partition, index, type, comparer); } else if (index + begin > partition) { QuickSelectList(source, partition + 1, end, index - partition + begin - 1, type, comparer); } else { return(source[partition]); } } throw new IndexOutOfRangeException(); }