Esempio n. 1
0
        public static void SelectionSort <T>(T[] arr) where T : IComparable <T>
        {
            Debug.Assert(0 < arr.Length, "SelectionSort: Array cannot 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]);
            }

            bool isSorted = AssertionsHelpingFunctions.IsSortedArray(arr);

            Debug.Assert(isSorted, "SelectionSort does not work correctly. Array is not sorted!");
        }
Esempio n. 2
0
        private static int BinarySearch <T>(T[] arr, T value, int startIndex, int endIndex)
            where T : IComparable <T>
        {
            Debug.Assert(0 < arr.Length, "BinarySearch: Array cannot be empty!");
            Debug.Assert(value != null, "BinarySearch: Searched value cannot be null!");
            Debug.Assert(0 <= startIndex && startIndex < arr.Length, "BinarySearch: Start index not in range!");
            Debug.Assert(0 <= endIndex && endIndex < arr.Length, "BinarySearch: End index not in range!");
            Debug.Assert(startIndex <= endIndex, "BinarySearch: Start index cannot be bigger than end index!");

            bool isSortedArray = AssertionsHelpingFunctions.IsSortedArray(arr);

            Debug.Assert(isSortedArray, "BinarySearch cannot be apply on unsorted array!");

            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 hasValue = AssertionsHelpingFunctions.HasValue(arr, value);

            Debug.Assert(hasValue, "BinarySearch returns -1, although searched value exist!");
            // Searched value not found
            return(-1);
        }
Esempio n. 3
0
        private static int FindMinElementIndex <T>(T[] arr, int startIndex, int endIndex)
            where T : IComparable <T>
        {
            Debug.Assert(0 < arr.Length, "FindMinElementIndex: Array cannot be empty!");
            Debug.Assert(0 <= startIndex && startIndex < arr.Length - 1, "FindMinElementIndex: Start index not in range!");
            Debug.Assert(0 < endIndex && endIndex < arr.Length, "FindMinElementIndex: End index not in range!");
            Debug.Assert(startIndex < endIndex, "FindMinElementIndex: Start index cannot be bigger or equal than end index!");

            int minElementIndex = startIndex;

            for (int i = startIndex + 1; i <= endIndex; i++)
            {
                if (arr[i].CompareTo(arr[minElementIndex]) < 0)
                {
                    minElementIndex = i;
                }
            }

            bool isMinElementIndex = AssertionsHelpingFunctions.IsMinElementIndex(arr, minElementIndex);

            Debug.Assert(isMinElementIndex, "FindMinElementIndex does not work correct. Invalid min element index!");

            return(minElementIndex);
        }