示例#1
0
        private static int Partation <T>(ListSlice <T> list) where T : System.IComparable <T>, System.IComparable
        {
            int left = 0, right = list.Count - 2;
            T   pivot = list[right + 1];

            while (left < right)
            {
                if (list[left].CompareTo(pivot) < 0)
                {
                    left++;
                    continue;
                }
                if (list[right].CompareTo(pivot) > 0)
                {
                    right--;
                    continue;
                }
                list.Swap(left, right);
                left++;
                right--;
            }

            int mid = list[right].CompareTo(pivot) > 0 ? right : right + 1;

            list.Swap(mid, list.Count - 1);

            return(mid);
        }
示例#2
0
 public static void BubbleSort <T>(ListSlice <T> list) where T : System.IComparable <T>, System.IComparable
 {
     for (int i = list.Count; i >= 1; i--)
     {
         for (int j = 1; j < i; j++)
         {
             list.SwapIfGreater(j - 1, j);
         }
     }
 }
示例#3
0
        public static void HeapSort <T>(ListSlice <T> list) where T : System.IComparable <T>, System.IComparable
        {
            for (int i = list.Count / 2 - 1; i >= 0; i--)
            {
                heapify(list, i);
            }

            for (int i = list.Count - 1; i >= 0; i--)
            {
                list.Swap(0, i);

                heapify(list.GetSlice(0, i), 0);
            }
        }
示例#4
0
 public static void SelectionSort <T>(ListSlice <T> list) where T : System.IComparable, System.IComparable <T>
 {
     for (int i = 1; i < list.Count; i++)
     {
         for (int j = i - 1; j >= 0; j--)
         {
             if (list[j + 1].CompareTo(list[j]) < 0)
             {
                 list.Swap(j + 1, j);
             }
             else
             {
                 continue;
             }
         }
     }
 }
示例#5
0
        private static void MergeInsertionSort <T>(ListSlice <T> list, int threshold = 100) where T : System.IComparable <T>, System.IComparable
        {
            if (list.Count > 10)
            {
                int mid   = list.Count / 2;
                var left  = list.GetSlice(0, mid);
                var right = list.GetSlice(mid, list.Count - mid);

                MergeInsertionSort(left);
                MergeInsertionSort(right);

                Merging(left, right);
            }
            else
            {
                InsertionSort(list);
            }
        }
示例#6
0
        private static void MergeSort <T>(ListSlice <T> list) where T : System.IComparable, System.IComparable <T>
        {
            if (list.Count > 2)
            {
                int mid   = list.Count / 2;
                var left  = list.GetSlice(0, mid);
                var right = list.GetSlice(mid, list.Count - mid);

                MergeSort(left);
                MergeSort(right);

                Merging(left, right);
            }
            else
            {
                list.SwapIfGreater(0, list.Count - 1);
            }
        }
示例#7
0
 public static void ShellSort <T>(ListSlice <T> list, int step) where T : System.IComparable, System.IComparable <T>
 {
     if (step > 0)
     {
         for (int i = 0; i < list.Count - step; i++)
         {
             if (list[i].CompareTo(list[i + step]) > 0)
             {
                 list.Swap(i, i + step);
             }
         }
         ShellSort(list, step / 2);
     }
     else
     {
         InsertionSort(list);
     }
 }
示例#8
0
 public static void InsertionSort <T>(ListSlice <T> list) where T : System.IComparable, System.IComparable <T>
 {
     for (int i = 0; i < list.Count; i++)
     {
         T   temp = list[i];
         int j    = i - 1;
         for (; j >= 0; j--)
         {
             if (temp.CompareTo(list[j]) < 0)
             {
                 list[j + 1] = list[j];
             }
             else
             {
                 break;
             }
         }
         list[j + 1] = temp;
     }
 }
示例#9
0
        private static void Merging <T>(ListSlice <T> left, ListSlice <T> right) where T : System.IComparable, System.IComparable <T>
        {
            T[] tempArray = new T[left.Count + right.Count];
            int counterG = 0, counterLeft = 0, counterRight = 0;

            while (counterLeft < left.Count && counterRight < right.Count)
            {
                if (left[counterLeft].CompareTo(right[counterRight]) < 0)
                {
                    tempArray[counterG] = left[counterLeft];
                    counterLeft++;
                }
                else
                {
                    tempArray[counterG] = right[counterRight];
                    counterRight++;
                }
                counterG++;
            }

            while (counterLeft < left.Count)
            {
                tempArray[counterG] = left[counterLeft];
                counterLeft++;
                counterG++;
            }
            while (counterRight < right.Count)
            {
                tempArray[counterG] = right[counterRight];
                counterRight++;
                counterG++;
            }

            for (int i = 0; i < tempArray.Length; i++)
            {
                left[i] = tempArray[i];
            }
        }
示例#10
0
        private static void heapify <T>(ListSlice <T> list, int i) where T : System.IComparable <T>, System.IComparable
        {
            int largest = i;
            int l       = 2 * i + 1;
            int r       = 2 * i + 2;

            if (l < list.Count && list[l].CompareTo(list[largest]) > 0)
            {
                largest = l;
            }

            if (r < list.Count && list[r].CompareTo(list[largest]) > 0)
            {
                largest = r;
            }

            if (largest != i)
            {
                list.Swap(i, largest);

                heapify(list, largest);
            }
        }
示例#11
0
        private static void QuickSort <T>(ListSlice <T> list) where T : System.IComparable <T>, System.IComparable
        {
            if (list.Count == 3)
            {
                list.SwapIfGreater(0, 2);
                list.SwapIfGreater(1, 2);
                list.SwapIfGreater(0, 1);
            }
            else if (list.Count == 2)
            {
                list.SwapIfGreater(0, 1);
            }
            else if (list.Count <= 16)
            {
                InsertionSort(list);
            }
            else if (list.Count > 3)
            {
                int mid = Partation(list);

                QuickSort(list.GetSlice(0, mid));
                QuickSort(list.GetSlice(mid + 1, list.Count - mid - 1));
            }
        }
示例#12
0
        public static void CombSort <T>(ListSlice <T> list) where T : System.IComparable <T>, System.IComparable
        {
            int  gap    = list.Count;
            bool sorted = false;

            while (!sorted)
            {
                if (gap < 1)
                {
                    sorted = true;
                    gap    = 1;
                }

                for (int i = 0; i < list.Count - gap; i++)
                {
                    if (list[i].CompareTo(list[i + gap]) > 0)
                    {
                        list.Swap(i, i + gap);
                        sorted = false;
                    }
                }
                gap = (int)(gap / 1.3f);
            }
        }