private static int BinarySearch <T>(T[] arr, T value, int startIndex, int endIndex) where T : IComparable <T> { Debug.Assert(arr != null, "Array could not be null!"); Debug.Assert(startIndex >= 0 && startIndex < arr.Length, string.Format("Start index must be between 0 and {0}!", arr.Length - 1)); Debug.Assert(endIndex == arr.Length - 1, string.Format("End index must be exactly {0}!", arr.Length - 1)); while (startIndex <= endIndex) { int midIndex = (startIndex + endIndex) / 2; if (arr[midIndex].Equals(value)) { Debug.Assert(arr[midIndex].CompareTo(value) == 0, "Incorrect index!"); 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; } } Debug.Assert(AssertionsHelperMethods.HasElement(arr, value), "The array has searched value!"); // Searched value not found return(-1); }
internal static void SelectionSort <T>(T[] arr) where T : IComparable <T> { Debug.Assert(arr != null, "Array could not be null!"); Debug.Assert(arr.Length != 0, "Array could not be empty!"); for (int index = 0; index < arr.Length - 1; index++) { int minElementIndex = FindMinElementIndex(arr, index, arr.Length - 1); Swap(ref arr[index], ref arr[minElementIndex]); } Debug.Assert(AssertionsHelperMethods.IsSorted(arr), "Array is not sorted!"); }
private static int FindMinElementIndex <T>(T[] arr, int startIndex, int endIndex) where T : IComparable <T> { Debug.Assert(arr != null, "Array could not be null!"); Debug.Assert(startIndex >= 0 && startIndex < arr.Length, string.Format("Start index must be between 0 and {0}!", arr.Length - 1)); Debug.Assert(endIndex == arr.Length - 1, string.Format("End index must be exactly {0}!", arr.Length - 1)); int minElementIndex = startIndex; for (int i = startIndex + 1; i <= endIndex; i++) { if (arr[i].CompareTo(arr[minElementIndex]) < 0) { minElementIndex = i; } } Debug.Assert(AssertionsHelperMethods.IsMinElement(arr, minElementIndex, startIndex), string.Format("index of the smallest element is not {0}!", minElementIndex)); return(minElementIndex); }