public static void SelectionSort <T>(T[] arr) where T : IComparable <T> { Debug.Assert(arr != null, "The array doesn't exist"); bool elementOfArrIsNull = AssertionHelpers.CheckArrayForNullElement(arr); Debug.Assert(elementOfArrIsNull, "At least one element of 'arr' is null"); for (int index = 0; index < arr.Length - 1; index++) { int minElementIndex = FindMinElementIndex(arr, index, arr.Length - 1); Swap(ref arr[index], ref arr[minElementIndex]); } bool isSorted = AssertionHelpers.ValidateSorting(arr); Debug.Assert(isSorted, "Sorting failed."); }
private static int BinarySearch <T>(T[] arr, T value, int startIndex, int endIndex) where T : IComparable <T> { int len = arr.Length; Debug.Assert(len >= startIndex, "startIndex is out of range."); Debug.Assert(len >= endIndex, "endIndex is out of range."); Debug.Assert(startIndex < endIndex, "endIndex is out of range."); 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; } } bool arrayContainsValue = AssertionHelpers.CheckArrayForValue(arr, value); Debug.Assert(!arrayContainsValue, "Invalid return reached. The element exists in the array!"); // Searched value not found return(-1); }
private static int FindMinElementIndex <T>(T[] arr, int startIndex, int endIndex) where T : IComparable <T> { int len = arr.Length; Debug.Assert(len >= startIndex, "startIndex is out of range."); Debug.Assert(len >= endIndex, "endIndex is out of range."); Debug.Assert(startIndex < endIndex, "endIndex is out of range."); int minElementIndex = startIndex; for (int i = startIndex + 1; i <= endIndex; i++) { if (arr[i].CompareTo(arr[minElementIndex]) < 0) { minElementIndex = i; } } bool isMinElementIndex = AssertionHelpers.ValidateMinElementIndex(arr, minElementIndex, startIndex, endIndex); Debug.Assert(isMinElementIndex, "minElementIndex is not the index of the minimum element."); return(minElementIndex); }