Esempio n. 1
0
        public static void sort3ways <T>(T[] arr, int l, int r) where T : IComparable
        {
            if (r <= l)
            {
                return;
            }
            int lt = l, gt = r, i = l + 1;
            T   v = arr[l];

            while (i <= gt)
            {
                int cmp = arr[i].CompareTo(v);
                if (cmp < 0)
                {
                    SortTestHelper.swap(arr, i, lt);
                    lt++;
                    i++;
                }
                else if (cmp == 0)
                {
                    i++;
                }
                else
                {
                    SortTestHelper.swap(arr, i, gt);
                    gt--;
                }
            }

            sort3ways(arr, l, lt - 1);
            sort3ways(arr, gt + 1, r);
        }
Esempio n. 2
0
        public static int partition <T>(T[] arr, int l, int r) where T : IComparable
        {
            int i = l, j = r + 1;

            while (true)
            {
                while (arr[++i].CompareTo(arr[l]) < 0)
                {
                    if (i == r)
                    {
                        break;
                    }
                }
                while (arr[--j].CompareTo(arr[l]) > 0)
                {
                    if (j == l)
                    {
                        break;
                    }
                }

                if (i >= j)
                {
                    break;
                }

                SortTestHelper.swap(arr, i, j);
            }
            SortTestHelper.swap(arr, l, j);
            return(j);
        }
Esempio n. 3
0
        public static int partition21 <T>(T[] arr, int l, int r) where T : IComparable
        {
            int i = l, j = r;

            while (i != j)
            {
                // 必须j先出动,因为j是寻找小于基准数的值,最后与基准数交换位置的值必须小于基准数的数。
                while (i < j && arr[j].CompareTo(arr[l]) >= 0)
                {
                    j--;
                }

                while (i < j && arr[i].CompareTo(arr[l]) <= 0)
                {
                    i++;
                }

                if (i < j)
                {
                    SortTestHelper.swap(arr, i, j);
                }
            }

            SortTestHelper.swap(arr, j, l);

            return(i);
        }
Esempio n. 4
0
        public static int partition22 <T>(T[] arr, int l, int r) where T : IComparable
        {
            int i = l + 1, j = r;

            while (true)
            {
                while (i <= r && arr[i].CompareTo(arr[l]) < 0)
                {
                    i++;
                }
                while (j >= l + 1 && arr[j].CompareTo(arr[l]) > 0)
                {
                    j--;
                }
                if (i > j)
                {
                    break;
                }
                SortTestHelper.swap(arr, i, j);
                i++;
                j--;
            }

            SortTestHelper.swap(arr, l, j);

            return(j);
        }
Esempio n. 5
0
        public static void sort <T>(T[] arr) where T : IComparable
        {
            int n = arr.Length;

            n = n / 2;

            while (n > 0)
            {
                for (int i = n; i < arr.Length; i++)
                {
                    for (int j = i; j >= n; j = j - n)
                    {
                        if (arr[j - n].CompareTo(arr[j]) > 0)
                        {
                            SortTestHelper.swap(arr, j - n, j);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                n = n / 2;
            }
        }
Esempio n. 6
0
        public static void sort <Item>(Item[] arr) where Item : IComparable
        {
            int n = arr.Length;

            for (int i = (n - 1 - 1) / 2; i >= 0; i--)
            {
                sink(arr, n, i);
            }

            for (int j = n - 1; j > 0; j--)
            {
                SortTestHelper.swap(arr, 0, j);
                sink(arr, j, 0);
            }
        }
Esempio n. 7
0
 public static void sort <T>(T[] arr) where T : IComparable
 {
     //循环n-1次,只需冒泡n-1个元素
     for (int i = 1; i <= arr.Length - 1; i++)
     {
         //通过对arr[0..n-i]内的相邻元素依次进行比较并交换位置,使arr[0..n-i]中最大元素移动至arr[1,n-i]末端。
         for (int j = 0; j < arr.Length - i; j++)
         {
             //比较相邻元素,如果arr[j-1]>arr[j]则交换位置
             if (arr[j].CompareTo(arr[j + 1]) > 0)
             {
                 SortTestHelper.swap(arr, j, j + 1);
             }
         }
     }
 }
        public static void sort <T>(T[] arr) where T : IComparable
        {
            for (int i = 0; i < arr.Length; i++)
            {
                int minIndex = i;

                for (int j = i; j < arr.Length; j++)
                {
                    if (arr[j].CompareTo(arr[minIndex]) <= 0)
                    {
                        minIndex = j;
                    }
                }

                SortTestHelper.swap(arr, i, minIndex);
            }
        }
Esempio n. 9
0
        //单路快速排序
        public static int partition1 <T>(T[] arr, int l, int r) where T : IComparable
        {
            int i = l + 1, j = l;

            while (i <= r)
            {
                if (arr[i].CompareTo(arr[l]) <= 0)
                {
                    j++;
                    SortTestHelper.swap(arr, j, i);
                }
                i++;
            }

            SortTestHelper.swap(arr, l, j);
            return(j);
        }
Esempio n. 10
0
 public static void insertSort(T[] arr, int l, int r)
 {
     for (int i = l + 1; i <= r; i++)
     {
         for (int j = i; j > l; j--)
         {
             if (arr[j - 1].CompareTo(arr[j]) > 0)
             {
                 SortTestHelper.swap(arr, j - 1, j);
             }
             else
             {
                 break;
             }
         }
     }
 }
Esempio n. 11
0
 public static void sortBasic <T>(T[] arr) where T : IComparable
 {
     for (int i = 1; i < arr.Length; i++)
     {
         for (int j = i; j > 0; j--)
         {
             if (arr[j - 1].CompareTo(arr[j]) > 0)
             {
                 SortTestHelper.swap(arr, j - 1, j);
             }
             else
             {
                 break;
             }
         }
     }
 }
Esempio n. 12
0
        public static void sink <Item>(Item[] arr, int n, int k) where Item : IComparable
        {
            while (k * 2 + 1 < n)
            {
                int j = k * 2 + 1;
                if (k * 2 + 2 < n && arr[j + 1].CompareTo(arr[j]) > 0)
                {
                    j++;
                }
                if (arr[j].CompareTo(arr[k]) > 0)
                {
                    SortTestHelper.swap(arr, j, k);

                    k = j;
                }
                else
                {
                    break;
                }
            }
        }
Esempio n. 13
0
        static void Main(string[] args)
        {
            int n = 1000000;

            int[] a = SortTestHelper.generateRandomArray(n, 0, n);
            int[] d = SortTestHelper.generateNearlyOrderedArray(n, 100);

            //SelectionSort.sort(a);
            //SortTestHelper.printArray(a);
            int[] b = new int[a.Length];
            int[] c = new int[a.Length];
            int[] e = new int[a.Length];
            int[] f = new int[a.Length];
            int[] g = new int[a.Length];
            int[] h = new int[a.Length];
            int[] i = new int[a.Length];
            int[] j = new int[a.Length];
            int[] k = new int[a.Length];
            int[] l = new int[a.Length];



            Array.Copy(a, b, a.Length);
            Array.Copy(a, c, a.Length);
            Array.Copy(a, e, a.Length);
            Array.Copy(a, f, a.Length);
            Array.Copy(a, h, a.Length);
            Array.Copy(a, i, a.Length);
            Array.Copy(a, j, a.Length);
            Array.Copy(a, k, a.Length);
            Array.Copy(a, l, a.Length);



            //   SortTestHelper.testSort("Selection Sort", SelectionSort.sort, a);
            //   SortTestHelper.testSort("Insertion Sort Basic", InsertionSort.sortBasic, b);
            SortTestHelper.testSort("Insertion Sort Advanced", InsertionSort.sortAdvanced, c);
            SortTestHelper.testSort("Insertion Sort Advanced Nearly Ordered", InsertionSort.sortAdvanced, d);
            // SortTestHelper.testSort("Bubble Sort", BubbleSort.sort, e);
            //SortTestHelper.testSort("Shell Sort", ShellSort.sort, f);
            SortTestHelper.testSort("Merge Sort", MergeSort <int> .sort, g);
            SortTestHelper.testSort("Merge Sort Advanced", MergeSortAdvanced <int> .sort, h);
            SortTestHelper.testSort("Merge Sort Buttom UP", MergeSortBU <int> .sort, i);
            SortTestHelper.testSort("Quick Sort", QuickSort.sort, j);
            SortTestHelper.testSort("Heap Sort", HeapSort.sort, k);
            SortTestHelper.testSort("Heap Sort - no aux array", HeapSort2.sort, l);



            //foreach (int o in l)
            //{
            //    Console.WriteLine(o);
            //}

            Console.WriteLine("------------------------");
            float[] z = { 3.1F, 2.2F, 3.2F, 1.1F };
            InsertionSort.sortBasic(z);

            foreach (float y in z)
            {
                Console.WriteLine(y);
            }

            Student[] stu = { new Student("A", 10), new Student("C", 30), new Student("B", 20), new Student("D", 20) };

            SelectionSort.sort <Student>(stu);
            foreach (Student y in stu)
            {
                Console.WriteLine(y);
            }
        }