private static int BinarySearch <T>(T[] arr, T value, int startIndex, int endIndex) where T : IComparable <T> { DebugAssertionHelper.CheckArraysLength(arr); DebugAssertionHelper.ValidateStartAndEndIndexesInArray(arr, startIndex, endIndex); while (startIndex <= endIndex) { int midIndex = (startIndex + endIndex) / 2; if (arr[midIndex].Equals(value)) { return(midIndex); } if (arr[midIndex].CompareTo(value) < 0) { // Search on the right half startIndex = midIndex + 1; } else { // Search on the right half endIndex = midIndex - 1; } } // Searched value not found return(-1); }
private static int FindMinElementIndex <T>(T[] arr, int startIndex, int endIndex) where T : IComparable <T> { DebugAssertionHelper.CheckArraysLength(arr); DebugAssertionHelper.ValidateStartAndEndIndexesInArray(arr, startIndex, endIndex); int minElementIndex = startIndex; for (int i = startIndex + 1; i <= endIndex; i++) { if (arr[i].CompareTo(arr[minElementIndex]) < 0) { minElementIndex = i; } } return(minElementIndex); }