//find nth smallest element. The element returned will have n elements smaller or equal to public static IComparable GetNthElement(IComparable[] inputArray, int n) { new Random().Shuffle(inputArray); int startIndex = 0; int endIndex = inputArray.Length - 1; while (endIndex > startIndex) { int j = QuickSort.Partition(inputArray, startIndex, endIndex); if (j == n) { return(inputArray[n]); } if (j > n) { endIndex = j - 1; } else { startIndex = j + 1; } } return(inputArray[n]); }