Example #1
0
        private static void MaxHeapify <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int heapSize, int index)
        {
            int left    = (index + 1) * 2 - 1;
            int right   = (index + 1) * 2;
            int largest = 0;

            if (left < heapSize && compare(get(left), get(index)) == Comparison.Greater)
            {
                largest = left;
            }
            else
            {
                largest = index;
            }
            if (right < heapSize && compare(get(right), get(largest)) == Comparison.Greater)
            {
                largest = right;
            }
            if (largest != index)
            {
                T temp = get(index);
                set(index, get(largest));
                set(largest, temp);
                MaxHeapify(compare, get, set, heapSize, largest);
            }
        }
Example #2
0
 /// <summary>Sorts an entire array in non-decreasing order using the slow sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">The method of compare for the sort.</param>
 /// <param name="array">The array to be sorted.</param>
 /// <remarks>Runtime: Omega(n), average(n*n!), O(infinity). Memory: in place. Stablity: no.</remarks>
 public static void Bogo <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     while (!BogoCheck(compare, get, set, start, end))
     {
         Shuffle(get, set, start, end);
     }
 }
Example #3
0
        /// <summary>Sorts an entire array in non-decreasing order using the odd-even sort algorithm.</summary>
        /// <typeparam name="T">The type of objects stored within the array.</typeparam>
        /// <param name="compare">The method of compare for the sort.</param>
        /// <param name="get">Delegate for getting a value at a specified index.</param>
        /// <param name="set">Delegate for setting a value at a specified index.</param>
        /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stablity: yes.</remarks>
        public static void OddEven <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
        {
            var sorted = false;

            while (!sorted)
            {
                sorted = true;
                for (var i = start + 1; i < end - 1; i += 2)
                {
                    if (compare(get(i), get(i + 1)) == Comparison.Greater)
                    {
                        T temp = get(i);
                        set(i, get(i + 1));
                        set(i + 1, temp);
                        sorted = false;
                    }
                }
                for (var i = start; i < end - 1; i += 2)
                {
                    if (compare(get(i), get(i + 1)) == Comparison.Greater)
                    {
                        T temp = get(i);
                        set(i, get(i + 1));
                        set(i + 1, temp);
                        sorted = false;
                    }
                }
            }
        }
Example #4
0
 internal SetIndexedProperty(RubyContext /*!*/ context, string /*!*/ name, CallInfo /*!*/ callInfo)
 {
     Assert.NotNull(context);
     _context   = context;
     _getMember = context.MetaBinderFactory.InteropGetMember(name);
     _setIndex  = context.MetaBinderFactory.InteropSetIndex(callInfo);
 }
Example #5
0
 /// <summary>Sorts an entire array in a randomized order.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="get">The get accessor of the structure to shuffle.</param>
 /// <param name="set">The set accessor of the structure to shuffle.</param>
 /// <param name="start">The starting index of the shuffle.</param>
 /// <param name="end">The ending index of the shuffle.</param>
 /// <remarks>Runtime: O(n). Memory: in place. Stable: N/A (not a comparative sort).</remarks>
 public static void Shuffle <T>(System.Random random, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     for (int i = start; i < end; i++)
     {
         int randomIndex = random.Next(start, end);
         T   temp        = get(i);
         set(i, get(randomIndex));
         set(randomIndex, temp);
     }
 }
Example #6
0
 private static bool BogoCheck <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     for (int i = start; i < end - 1; i++)
     {
         if (compare(get(i), get(i + 1)) == Comparison.Greater)
         {
             return(false);
         }
     }
     return(true);
 }
Example #7
0
 public void changeIndex(int index)
 {
     if (serversCBox.InvokeRequired)
     {
         SetIndex setIndex = new SetIndex(changeIndex);
         this.Invoke(setIndex, index);
     }
     else
     {
         serversCBox.SelectedIndex = index;
     }
 }
Example #8
0
 /// <summary>Sorts an entire array in non-decreasing order using the insertion sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">Returns positive if left greater than right.</param>
 /// <param name="get">Delegate for getting a value at a specified index.</param>
 /// <param name="set">Delegate for setting a value at a specified index.</param>
 /// <param name="start">The starting index of the sort.</param>
 /// <param name="end">The ending index of the sort.</param>
 /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stablity: yes.</remarks>
 public static void Insertion <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     for (int i = start + 1; i < end; i++)
     {
         T   temp = get(i);
         int j;
         for (j = i; j > start && compare(get(j - 1), temp) == Comparison.Greater; j--)
         {
             set(j, get(j - 1));
         }
         set(j, temp);
     }
 }
Example #9
0
 /// <summary>Sorts an entire array in non-decreasing order using the bubble sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">The compare function (returns a positive value if left is greater than right).</param>
 /// <param name="get">Delegate for getting a value at a specified index.</param>
 /// <param name="set">Delegate for setting a value at a specified index.</param>
 /// <param name="start">The starting index of the sort.</param>
 /// <param name="end">The ending index of the sort.</param>
 /// <remarks>Runtime: Omega(n), average(n^2), O(n^2). Memory: in place. Stability: yes.</remarks>
 public static void Bubble <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     for (int i = start; i < end; i++)
     {
         for (int j = start; j < end - 1; j++)
         {
             if (compare(get(j), get(j + 1)) == Comparison.Greater)
             {
                 T temp = get(j + 1);
                 set(j + 1, get(j));
                 set(j, temp);
             }
         }
     }
 }
Example #10
0
 /// <summary>Sorts an entire array in non-decreasing order using the selection sort algoritm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">Returns negative if the left is less than the right.</param>
 /// <param name="get">Delegate for getting a value at a specified index.</param>
 /// <param name="set">Delegate for setting a value at a specified index.</param>
 /// <param name="start">The starting index of the sort.</param>
 /// <param name="end">The ending index of the sort.</param>
 /// <remarks>Runtime: Omega(n^2), average(n^2), O(n^2). Memory: in place. Stablity: no.</remarks>
 public static void Selection <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     for (int i = 0; i < end - 1; i++)
     {
         int min_idx = i;
         for (int j = i + 1; j < end; j++)
         {
             if (compare(get(j), get(min_idx)) == Comparison.Less)
             {
                 min_idx = j;
             }
         }
         T temp = get(min_idx);
         set(min_idx, get(i));
         set(i, temp);
     }
 }
Example #11
0
        /// <summary>Sorts an entire array in non-decreasing order using the heap sort algorithm.</summary>
        /// <typeparam name="T">The type of objects stored within the array.</typeparam>
        /// <param name="compare">The method of compare for the sort.</param>
        /// <param name="get">Delegate for getting a value at a specified index.</param>
        /// <param name="set">Delegate for setting a value at a specified index.</param>
        /// <param name="start">The starting index of the sort.</param>
        /// <param name="end">The ending index of the sort.</param>
        /// <remarks>Runtime: Omega(n*ln(n)), average(n*ln(n)), O(n^2). Memory: in place. Stablity: no.</remarks>
        public static void Heap <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
        {
            int heapSize = end - start;

            for (int i = (heapSize - 1) / 2; i >= 0; i--)
            {
                MaxHeapify(compare, get, set, heapSize, i);
            }
            for (int i = end - 1; i > start; i--)
            {
                T temp = get(0);
                set(0, get(i));
                set(i, temp);
                heapSize--;
                MaxHeapify(compare, get, set, heapSize, 0);
            }
        }
Example #12
0
 /// <summary>Sorts an entire array of in non-decreasing order using the slow sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">The method of compare for the sort.</param>
 /// <param name="array">The array to be sorted</param>
 /// <remarks>Runtime: Omega(n), average(n*n!), O(n*n!). Memory: in place. Stablity: N/A (not yet analyzed).</remarks>
 public static void Slow <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     throw new System.NotImplementedException();
     Slow_Recursive(compare, get, set, start, end);
 }
Example #13
0
        private static void Slow_Recursive <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int i, int j)
        {
            if (i >= j)
            {
                return;
            }
            int m = (i + j) / 2;

            Slow_Recursive(compare, get, set, i, m);
            Slow_Recursive(compare, get, set, m + 1, j);
            if (compare(get(m), get(j)) == Comparison.Less)
            {
                T temp = get(j);
                set(j, get(m));
                set(m, temp);
            }
            Slow_Recursive(compare, get, set, i, j - 1);
        }
Example #14
0
 /// <summary>Shuffles the elements of an IList into random order.</summary>
 /// <typeparam name="T">The generic type of the IList.</typeparam>
 /// <param name="random">The random algorithm for index generation.</param>
 /// <param name="get">The get accessor for the structure to shuffle.</param>
 /// <param name="assign">The set accessor for the structure to shuffle.</param>
 /// <param name="start">The starting index of the shuffle.</param>
 /// <param name="end">The </param>
 public static void Shuffle <T>(this Random random, GetIndex <T> get, SetIndex <T> assign, int start, int end)
 {
     Sort.Shuffle(random, get, assign, start, end);
 }
Example #15
0
 internal SetIndexedProperty(RubyContext/*!*/ context, string/*!*/ name, CallInfo/*!*/ callInfo) {
     Assert.NotNull(context);
     _context = context;
     _getMember = context.MetaBinderFactory.InteropGetMember(name);
     _setIndex = context.MetaBinderFactory.InteropSetIndex(callInfo);
 }
Example #16
0
File: Sort.cs Project: Thaina/Towel
 /// <summary>Sorts an entire array in non-decreasing order using the quick sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">The method of compare to be sorted by.</param>
 /// <param name="get">Delegate for getting a value at a specified index.</param>
 /// <param name="set">Delegate for setting a value at a specified index.</param>
 /// <param name="start">The starting index of the sort.</param>
 /// <param name="end">The ending index of the sort.</param>
 /// <remarks>Runtime: Omega(n*ln(n)), average(n*ln(n)), O(n^2). Memory: ln(n). Stablity: no.</remarks>
 public static void Quick <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end) =>
 Quick_Recursive(compare, get, set, start, end - start + 1);
Example #17
0
 /// <summary>Sorts an entire array in a randomized order.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="get">The get accessor of the structure to shuffle.</param>
 /// <param name="set">The set accessor of the structure to shuffle.</param>
 /// <param name="start">The starting index of the shuffle.</param>
 /// <param name="end">The ending index of the shuffle.</param>
 /// <remarks>Runtime: O(n). Memory: in place. Stable: N/A (not a comparative sort).</remarks>
 public static void Shuffle <T>(GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     Shuffle(new System.Random(), get, set, start, end);
 }
Example #18
0
 private static void Quick_Recursive <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int len)
 {
     if (len > 1)
     {
         T   pivot = get(start);
         int i     = start;
         int j     = start + len - 1;
         int k     = j;
         while (i <= j)
         {
             if (compare(get(j), pivot) == Comparison.Less)
             {
                 T temp = get(i);
                 set(i++, get(j));
                 set(j, temp);
             }
             else if (compare(get(j), pivot) == Comparison.Equal)
             {
                 j--;
             }
             else
             {
                 T temp = get(k);
                 set(k--, get(j));
                 set(j--, temp);
             }
         }
         Quick_Recursive(compare, get, set, start, i - start);
         Quick_Recursive(compare, get, set, k + 1, start + len - (k + 1));
     }
 }
Example #19
0
 /// <summary>Sorts up to an array in non-decreasing order using the merge sort algorithm.</summary>
 /// <typeparam name="T">The type of objects stored within the array.</typeparam>
 /// <param name="compare">Returns zero or negative if the left is less than or equal to the right.</param>
 /// <param name="get">Delegate for getting a value at a specified index.</param>
 /// <param name="set">Delegate for setting a value at a specified index.</param>
 /// <param name="start">The starting index of the sort.</param>
 /// <param name="end">The ending index of the sort.</param>
 /// <remarks>Runtime: Omega(n*ln(n)), average(n*ln(n)), O(n*ln(n)). Memory: n. Stablity: yes.</remarks>
 public static void Merge <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int end)
 {
     Merge_Recursive(compare, get, set, start, end - start);
 }
Example #20
0
 private static void Merge_Recursive <T>(Compare <T> compare, GetIndex <T> get, SetIndex <T> set, int start, int len)
 {
     if (len > 1)
     {
         int half = len / 2;
         Merge_Recursive(compare, get, set, start, half);
         Merge_Recursive(compare, get, set, start + half, len - half);
         T[] sorted = new T[len];
         int i      = start;
         int j      = start + half;
         int k      = 0;
         while (i < start + half && j < start + len)
         {
             if (compare(get(i), get(j)) == Comparison.Greater)
             {
                 sorted[k++] = get(j++);
             }
             else
             {
                 sorted[k++] = get(i++);
             }
         }
         for (int h = 0; h < start + half - i; h++)
         {
             sorted[k + h] = get(i + h);
         }
         for (int h = 0; h < start + len - j; h++)
         {
             sorted[k + h] = get(j + h);
         }
         for (int h = 0; h < len; h++)
         {
             set(start + h, sorted[0 + h]);
         }
     }
 }