Exemple #1
0
        public static void Process()
        {
            string input    = "";
            string sortType = "";

            Console.WriteLine("Please enter your input numbers separated by comma: ");
            input = Console.ReadLine();
            Console.WriteLine("Thank you. Please enter mode of sorting\n1. Quick Sort\n2. Merge Sort\n3. Heap Sort\n4. Bubble Sort");
            sortType = Console.ReadLine();

            List <int>   unsorted    = input.Split(',').Select(int.Parse).ToList();
            ISortFactory sortFactory = new SortFactory();
            ISortedList  sortedList  = sortFactory.SortedList(sortType);


            (sortedList.Sort(unsorted)).ForEach(Console.WriteLine);

            Console.WriteLine("Do you want to continue...");

            switch (Console.ReadLine())
            {
            case "Y":
                Process();
                break;
            }
        }
Exemple #2
0
        public SortedReadOnlyList(ISortedList <T> sortedData)
        {
            Contract.Requires(sortedData != null);
            Contract.Requires(sortedData.Comparer != null);

            this.data2    = sortedData;
            this.comparer = sortedData.Comparer;
        }
Exemple #3
0
        /// <summary>
        /// Searches a sorted list using a given finder function, returning the index of the last matching item if found. If not found, the return value is -1. The finder function must be compatible with the comparer used to sort the list.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="finder">The finder function to use to find the item. This function should return 0 for a match, a negative value (meaning "search lower") if its parameter is too large, or a positive value (meaning "search higher") if its parameter is too small.</param>
        /// <returns>The index of the last item that causes <paramref name="finder"/> to return 0, if any; otherwise, -1.</returns>
        public static int LastIndexOf <T>(this ISortedList <T> list, Func <T, int> finder)
        {
            int ret = UpperBound(list, finder);

            if (ret >= 0)
            {
                return(ret - 1);
            }

            return(-1);
        }
Exemple #4
0
        /// <summary>
        /// Inserts a value into a sorted list in-place, maintaining the sort order. Returns the index of the inserted item. To "insert" an item without modifying the source list, call <see cref="O:SortedEnumerableExtensions.MergeSorted"/>.
        /// </summary>
        /// <typeparam name="T">The type of object contained in the list.</typeparam>
        /// <param name="list">The sorted list into which to insert.</param>
        /// <param name="item">The item to insert into the list.</param>
        /// <returns>The index at which the new item was inserted.</returns>
        public static int Insert <T>(this ISortedList <T> list, T item)
        {
            int index = list.BinarySearch(item);

            if (index < 0)
            {
                index = ~index;
            }

            list.Insert(index, item);
            return(index);
        }
Exemple #5
0
        /// <summary>
        /// Searches a sorted list using a given finder function, returning the index one past the last matching item if found. If not found, the return value is the bitwise complement of the next element larger than the value. The finder function must be compatible with the comparer used to sort the list.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="finder">The finder function to use to find the item. This function should return 0 for a match, a negative value (meaning "search lower") if its parameter is too large, or a positive value (meaning "search higher") if its parameter is too small.</param>
        /// <returns>The index one past the last item that causes <paramref name="finder"/> to return 0, if any; otherwise, the bitwise complement of the index one past the last item that causes <paramref name="finder"/> to return a positive result.</returns>
        public static int UpperBound <T>(this ISortedList <T> list, Func <T, int> finder)
        {
            int begin = 0, end = list.Count;

            int mid = BinarySearchCore(list, finder, ref begin, ref end);

            if (mid < 0)
            {
                return(mid);
            }

            UpperBoundCore(list, finder, mid, ref end);

            return(end);
        }
Exemple #6
0
        /// <summary>
        /// Searches a sorted list using a given finder function. The finder function must be compatible with the comparer used to sort the list.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="finder">The finder function to use to find the item. This function should return 0 for a match, a negative value (meaning "search lower") if its parameter is too large, or a positive value (meaning "search higher") if its parameter is too small.</param>
        /// <param name="begin">The lower bound of the range of values causing <paramref name="finder"/> to return 0. [begin, end) may be an empty range.</param>
        /// <param name="end">The upper bound of the range of values causing <paramref name="finder"/> to return 0. [begin, end) may be an empty range.</param>
        public static void EqualRange <T>(this ISortedList <T> list, Func <T, int> finder, out int begin, out int end)
        {
            begin = 0;
            end   = list.Count;

            int mid = BinarySearchCore(list, finder, ref begin, ref end);

            if (mid < 0)
            {
                begin = ~mid;
                end   = ~mid;
                return;
            }

            LowerBoundCore(list, finder, ref begin, mid);
            UpperBoundCore(list, finder, mid, ref end);
        }
Exemple #7
0
        /// <summary>
        /// Searches a sorted list for all instances of a given value.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="item">The item to search for in the list.</param>
        /// <param name="begin">The lower bound of the range of matching values. [begin, end) may be an empty range.</param>
        /// <param name="end">The upper bound of the range of matching values. [begin, end) may be an empty range.</param>
        public static void EqualRange <T>(this ISortedList <T> list, T item, out int begin, out int end)
        {
            IComparer <T> comparer = list.Comparer;

            list.EqualRange(x => comparer.Compare(item, x), out begin, out end);
        }
Exemple #8
0
        /// <summary>
        /// Searches a sorted list for a given value, returning the index one past the last matching item if found. If not found, the return value is the bitwise complement of the next element larger than the value.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="item">The item to search for in the list.</param>
        /// <returns>The index one past the last occurence of <paramref name="item"/> if it was in the list; otherwise, the bitwise complement of the next larger element in the list.</returns>
        public static int UpperBound <T>(this ISortedList <T> list, T item)
        {
            IComparer <T> comparer = list.Comparer;

            return(list.UpperBound(x => comparer.Compare(item, x)));
        }
Exemple #9
0
        /// <summary>
        /// Searches a sorted list using a given finder function. If not found, the return value is the bitwise complement of the next element larger than the value. The finder function must be compatible with the comparer used to sort the list.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="finder">The finder function to use to find the item. This function should return 0 for a match, a negative value (meaning "search lower") if its parameter is too large, or a positive value (meaning "search higher") if its parameter is too small.</param>
        /// <returns>The index of an item that causes <paramref name="finder"/> to return 0, if any; otherwise, the bitwise complement of the next larger element in the list.</returns>
        public static int BinarySearch <T>(this ISortedList <T> list, Func <T, int> finder)
        {
            int begin = 0, end = list.Count;

            return(BinarySearchCore(list, finder, ref begin, ref end));
        }
Exemple #10
0
 /// <summary>
 /// Returns a sliced list, which acts as a window into a subset of the original list.
 /// </summary>
 /// <typeparam name="T">The type of object contained in the list.</typeparam>
 /// <param name="list">The list to slice.</param>
 /// <param name="offset">The offset into the list at which the slice begins. Must be a valid index into the source list, or equal to the count of the source list.</param>
 /// <returns>A list that is a slice of the source list.</returns>
 public static ISortedList <T> Skip <T>(this ISortedList <T> list, int offset)
 {
     return(new SortedListWrapper <T>(ListExtensions.Skip(list, offset), list.Comparer));
 }
Exemple #11
0
 /// <summary> Returns the first occurence in specified list that matches the specified element according to the specified comparer. </summary>
 /// <typeparam name="T"> The type of the elements in the list. </typeparam>
 /// <typeparam name="U"> The type of the element to match elements in the list. </typeparam>
 /// <param name="sortedList"> The list to search in for a match. Must be transitive over the specified comparer with specified element. </param>
 /// <param name="element"> The element to search the match of. </param>
 /// <param name="comparer"> The comparer determining whether a </param>
 /// <returns> an occurence in the sorted list matching the specified element, or default(T) if no match was found. </returns>
 public static T FirstOrDefault <T, U>(this ISortedList <T> sortedList, U element, Func <T, U, int> comparer)
 {
     return(FirstOrDefault <T, U>(i => sortedList[i], sortedList.Count, element, comparer));
 }
Exemple #12
0
        /// <summary>
        /// Searches a sorted list for a given value, returning the index of the last matching item if found. If not found, the return value is -1.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="item">The item to search for in the list.</param>
        /// <returns>The index of the last occurence of <paramref name="item"/>, if any; otherwise, -1.</returns>
        public static int LastIndexOf <T>(this ISortedList <T> list, T item)
        {
            IComparer <T> comparer = list.Comparer;

            return(LastIndexOf <T>(list, x => comparer.Compare(item, x)));
        }
Exemple #13
0
 /// <summary>
 /// Searches a sorted list using a given finder function. The finder function must be compatible with the comparer used to sort the list.
 /// </summary>
 /// <typeparam name="T">The type of items in the list.</typeparam>
 /// <param name="list">The sorted list.</param>
 /// <param name="finder">The finder function to use to find the item. This function should return 0 for a match, a negative value (meaning "search lower") if its parameter is too large, or a positive value (meaning "search higher") if its parameter is too small.</param>
 /// <returns><c>true</c> if there is at least one item that causes <paramref name="finder"/> to return 0, if any; otherwise, <c>false</c>.</returns>
 public static bool Contains <T>(this ISortedList <T> list, Func <T, int> finder)
 {
     return(BinarySearch(list, finder) >= 0);
 }
Exemple #14
0
 /// <summary>
 /// Returns a sliced list, which acts as a window into a subset of the original list.
 /// </summary>
 /// <typeparam name="T">The type of object contained in the list.</typeparam>
 /// <param name="list">The list to slice.</param>
 /// <param name="count">The number of elements in the slice. May not be less than zero. If count is greater than 0, then every value in the range [0, count) must be valid indexes into the source list.</param>
 /// <returns>A list that is a slice of the source list.</returns>
 public static ISortedList <T> Take <T>(this ISortedList <T> list, int count)
 {
     return(new SortedListWrapper <T>(ListExtensions.Take(list, count), list.Comparer));
 }
Exemple #15
0
 /// <summary>
 /// Steps through a list using a specified step size.
 /// </summary>
 /// <typeparam name="T">The type of object contained in the list.</typeparam>
 /// <param name="list">The list to step through.</param>
 /// <param name="step">The step size. Must be greater than 0.</param>
 /// <returns>The stepped list.</returns>
 public static ISortedList <T> Step <T>(this ISortedList <T> list, int step)
 {
     return(new SortedListWrapper <T>(ListExtensions.Step(list, step), list.Comparer));
 }
Exemple #16
0
 /// <summary>
 /// Returns a list that acts as though it has been reversed, with a reversed comparison object.
 /// </summary>
 /// <typeparam name="T">The type of elements contained in the list.</typeparam>
 /// <param name="list">The source list.</param>
 /// <returns>A list sorted in reverse order of the original list.</returns>
 public static ISortedList <T> Reverse <T>(this ISortedList <T> list)
 {
     // Reverse the list and its comparison object
     return(new SortedListWrapper <T>(ListExtensions.Reverse(list), A.Comparer <T>((x, y) => list.Comparer.Compare(y, x))));
 }
Exemple #17
0
        /// <summary>
        /// Searches a sorted list for a given value, returning its index if found. If not found, the return value is the bitwise complement of the next element larger than the value.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="item">The item to search for in the list.</param>
        /// <returns>The index of <paramref name="item"/> if it was in the list; otherwise, the bitwise complement of the next larger element in the list.</returns>
        public static int BinarySearch <T>(this ISortedList <T> list, T item)
        {
            IComparer <T> comparer = list.Comparer;

            return(list.BinarySearch(x => comparer.Compare(item, x)));
        }
Exemple #18
0
 /// <summary>
 /// Returns the source typed as <see cref="ISortedList{T}"/>. This method has no effect other than to restrict the compile-time type of an object implementing <see cref="ISortedList{T}"/>.
 /// </summary>
 /// <typeparam name="T">The type of the elements of <paramref name="list"/>.</typeparam>
 /// <param name="list">The source list.</param>
 /// <returns>The source list, typed as <see cref="ISortedList{T}"/>.</returns>
 public static ISortedList <T> AsSortedList <T>(this ISortedList <T> list)
 {
     return(list);
 }
Exemple #19
0
 public static int IndexOf <T>(this ISortedList <T> list, T element)
 {
     return(IndexOf <T>(i => list[i], list.Count, element, list.Comparer));
 }