Exemplo n.º 1
0
 public void BubbleSortTest()
 {
     int[] array  = { 10, 23, 1, 33, 100, 23 };
     int[] result = { 1, 10, 23, 23, 33, 100 };
     SortUtil.BubbleSort(array);
     CollectionAssert.Equals(result, array);
 }
Exemplo n.º 2
0
        private static void Sort(string[] a, int lo, int hi, int d)
        {
            if (lo >= hi)
            {
                return;
            }

            var c = charAt(a[lo], d);

            int i = lo, lt = lo, gt = hi;

            while (i <= gt)
            {
                var cmp = c.CompareTo(charAt(a[i], d));
                if (cmp > 0)
                {
                    SortUtil.Exchange(a, i++, lt++);
                }
                else if (cmp < 0)
                {
                    SortUtil.Exchange(a, i, gt--);
                }
                else
                {
                    i++;
                }
            }

            Sort(a, lo, lt - 1, d);
            if (c >= 0)
            {
                Sort(a, lt, gt, d + 1);
            }
            Sort(a, gt + 1, hi, d);
        }
Exemplo n.º 3
0
 /// <summary>
 /// ソートパラメータ設定(ユニット)
 /// </summary>
 public virtual void setSortParamUnit(UnitGridParam unit_param)
 {
     unit_param.element_int  = SortUtil.GetSortNumElement(unit_param.element);
     unit_param.kind_int     = SortUtil.GetSortNumKind(unit_param.kind);
     unit_param.sub_kind_int = SortUtil.GetSortNumKind(unit_param.sub_kind);
     UnitParam = unit_param;
 }
Exemplo n.º 4
0
        public void BubbleSortTest3()
        {
            XmlSerializer z;

            int[] array = null;
            SortUtil.BubbleSort(array);
        }
Exemplo n.º 5
0
        public static int Partition <T>(T[] a, int lo, int hi, Comparison <T> compareTo)
        {
            int i = lo;
            int j = hi + 1;

            while (true)
            {
                while (SortUtil.IsLessThan(a[++i], a[lo], compareTo))
                {
                    if (i == hi)
                    {
                        break;
                    }
                }
                while (SortUtil.IsLessThan(a[lo], a[--j], compareTo))
                {
                    if (j == lo)
                    {
                        break;
                    }
                }

                if (i >= j)
                {
                    break;
                }

                SortUtil.Exchange(a, i, j);
            }

            SortUtil.Exchange(a, lo, j);
            return(j);
        }
Exemplo n.º 6
0
        public static void Sort <T>(T[] a, Comparison <T> compare)
        {
            var n = a.Length;
            var h = 1;

            while (h < n / 3)
            {
                h = 3 * h + 1;
            }

            var step = h;

            while (step > 0)
            {
                for (var i = step; i < n; i++)
                {
                    for (var j = i; j >= step; j -= step)
                    {
                        if (SortUtil.IsLessThan(a[j], a[j - step], compare))
                        {
                            SortUtil.Exchange(a, j, j - step);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                step--;
            }
        }
Exemplo n.º 7
0
        public static int IndexOf <T>(T[] a, T v) where T : IComparable <T>
        {
            var lo = 0;
            var hi = a.Length - 1;

            int comparison(T a1, T a2) => a1.CompareTo(a2);

            while (lo <= hi)
            {
                int mid = lo + (hi - lo) / 2;
                if (SortUtil.IsLessThan(a[mid], v, comparison))
                {
                    lo = mid + 1;
                }
                else if (SortUtil.IsGreaterThan(a[mid], v, comparison))
                {
                    hi = mid - 1;
                }
                else
                {
                    return(mid);
                }
            }
            return(-1);
        }
Exemplo n.º 8
0
        private static void Merge <T>(T[] a, T[] aux, int lo, int mid, int hi, Comparison <T> compare)
        {
            int i, j;

            for (i = lo; i <= hi; ++i)
            {
                aux[i] = a[i];
            }

            i = lo;
            j = mid;
            for (var k = lo; k <= hi; ++k)
            {
                if (i >= mid)
                {
                    a[k] = aux[j++];
                }
                else if (j >= hi)
                {
                    a[k] = aux[i++];
                }
                else if (SortUtil.IsLessThan(aux[i], aux[j], compare))
                {
                    a[k] = aux[i++];
                }
                else
                {
                    a[k] = aux[j++];
                }
            }
        }
Exemplo n.º 9
0
 public void BubbleSortTest2()
 {
     int[] array  = {};
     int[] result = {};
     SortUtil.BubbleSort(array);
     CollectionAssert.Equals(result, array);
 }
Exemplo n.º 10
0
        public void SelectionSort_SortsCorrectly()
        {
            var input = TestUtil.RandomArray(TestUtil.ARR_SIZE);

            var result = MergeSort.Sort(input);

            Assert.IsTrue(SortUtil.IsSorted(result));
        }
Exemplo n.º 11
0
        public static string MoveDown(string json, long topicId)
        {
            List <StickyTopic>     topics = GetTopics(json);
            StickyTopic            t      = getById(topics, topicId);
            SortUtil <StickyTopic> s      = new SortUtil <StickyTopic>(t, topics);

            s.MoveDown();
            return(Json.ToStringList(s.GetOrderedList()));
        }
Exemplo n.º 12
0
        public static String MoveDown(String json, int topicId)
        {
            List <StickyTopic>     topics = GetTopics(json);
            StickyTopic            t      = getById(topics, topicId);
            SortUtil <StickyTopic> s      = new SortUtil <StickyTopic>(t, topics);

            s.MoveDown();
            return(SimpleJsonString.ConvertList(s.GetOrderedList()));
        }
Exemplo n.º 13
0
        public void DecreaseKey(int index, T item)
        {
            int p = qp[index];

            if (SortUtil.IsGreaterThan(keys[index], item))
            {
                keys[index] = item;
                Swim(p);
            }
        }
Exemplo n.º 14
0
        public static void Shuffle <T>(T[] a)
        {
            var random = new Random();

            for (var i = 1; i < a.Length; ++i)
            {
                var j = random.Next(i + 1);
                SortUtil.Exchange(a, i, j);
            }
        }
Exemplo n.º 15
0
        public virtual IList <T> KeysSortedByValue(int count)
        {
            List <T> list = new List <T>();

            foreach (object obj in SortUtil.GetTopsByValue(map, count).Keys)
            {
                list.Add((T)obj);
            }
            return(list);
        }
    /// <summary>
    /// 产生符合泊松分布的随机数组。
    /// </summary>
    /// <param name="n">随机数组的大小。</param>
    /// <returns>符合泊松分布的数组。</returns>
    public static double[] GetPossionDistributionArray(int n)
    {
        var array = new double[n];

        for (var i = 0; i < n; i++)
        {
            array[i] = SortUtil.Poission(20);
        }
        return(array);
    }
    /// <summary>
    /// 产生符合几何分布的随机数组。
    /// </summary>
    /// <param name="n">随机数组的大小。</param>
    /// <param name="p">几何分布的概率。</param>
    /// <returns>符合几何分布的随机数组。</returns>
    public static double[] GetGeometricDistributionArray(int n, double p)
    {
        var array = new double[n];

        for (var i = 0; i < n; i++)
        {
            array[i] = SortUtil.Geometry(p);
        }
        return(array);
    }
    /// <summary>
    /// 产生符合指定概率的离散分布的随机数组。
    /// </summary>
    /// <param name="n">随机数组的大小。</param>
    /// <param name="probabilities">各取值的概率数组。</param>
    /// <returns>符合指定概率的离散分布的随机数组。</returns>
    public static double[] GetDiscretDistributionArray(int n, double[] probabilities)
    {
        var array = new double[n];

        for (var i = 0; i < n; i++)
        {
            array[i] = SortUtil.Discrete(probabilities);
        }
        return(array);
    }
    /// <summary>
    /// 获取符合标准正态分布的 double 数组。
    /// </summary>
    /// <param name="n">数组大小。</param>
    /// <returns>符合标准正态分布的 double 数组。</returns>
    public static double[] GetNormalDistributionArray(int n)
    {
        var array = new double[n];

        for (var i = 0; i < n; i++)
        {
            array[i] = SortUtil.Normal(0, 1);
        }
        return(array);
    }
Exemplo n.º 20
0
 public static void QuickSort()
 {
     Console.Write("Sorted Values, Quick Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             SortUtil.QuickSort(arr, 0, arr.Length - 1);
         }
     });
 }
Exemplo n.º 21
0
        public int DelMin()
        {
            var item = pq[1];

            SortUtil.Exchange(pq, 1, N);
            qp[pq[1]] = 1;
            qp[pq[N]] = N;
            N--;
            Sink(1);
            return(item);
        }
Exemplo n.º 22
0
 public static void InsertionSort()
 {
     Console.Write("Sorted Values, Insertion Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             SortUtil.InsertSort(arr);
         }
     });
 }
Exemplo n.º 23
0
 public static void QuickSort()
 {
     Console.Write("Random Values, Quick Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             FillArrayWithRandomValues();
             SortUtil.QuickSort(arr, 0, arr.Length - 1);
         }
     });
 }
Exemplo n.º 24
0
 public static void InsertionSort()
 {
     Console.Write("Random Values, Insertion Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             FillArrayWithRandomValues();
             SortUtil.InsertSort(arr);
         }
     });
 }
Exemplo n.º 25
0
 public static void Double()
 {
     Console.Write("Values-Double, Selection Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             var arr = new double[] { 3.4, 2.4, 4.3, 5.1, 1.3, 8.7, 9.4, 3.34, 2.23, 4.345 };
             SortUtil.SelectionSort(arr);
         }
     });
 }
Exemplo n.º 26
0
 public static void Int()
 {
     Console.Write("Values-Int, Selection Sort:\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             var arr = new int[] { 3, 2, 4, 5, 1, 8, 9, 3, 2, 4 };
             SortUtil.SelectionSort(arr);
         }
     });
 }
Exemplo n.º 27
0
 public static void Int()
 {
     Console.Write("Values-Int, Quick Sort:\t\t");
     Display.ExecutionTime(() =>
     {
         for (int i = 0; i < LOOP_END; i++)
         {
             var arr = new int[] { 3, 2, 4, 5, 1, 8, 9, 3, 2, 4 };
             SortUtil.QuickSort(arr, 0, arr.Length - 1);
         }
     });
 }
Exemplo n.º 28
0
            public QueueEnumerator(T[] s, int N)
            {
                s           = (T[])s.Clone();
                this.values = new T[N];
                int k = 0;

                while (N > 0)
                {
                    values[k++] = s[1];
                    SortUtil.Exchange(s, 1, N--);
                    Sink(s, 1, N);
                }
            }
Exemplo n.º 29
0
    static void Main()
    {
        var arr = new int[] { 3, 2, 4, 5, 1, 8, 9, 3, 2, 4 };

        SortUtil.QuickSort(arr, 0, arr.Length - 1);
        Console.WriteLine("After Quick Sort: {0}", string.Join(", ", arr));

        arr = new int[] { 3, 2, 4, 5, 1, 8, 9, 3, 2, 4 };
        SortUtil.InsertSort(arr);
        Console.WriteLine("After Insert Sort: {0}", string.Join(", ", arr));

        arr = new int[] { 3, 2, 4, 5, 1, 8, 9, 3, 2, 4 };
        SortUtil.SelectionSort(arr);
        Console.WriteLine("After Selection Sort: {0}", string.Join(", ", arr));

        Console.WriteLine("-----------------------------");

        CompareQuickSort.Int();
        CompareQuickSort.Double();
        CompareQuickSort.String();

        Console.WriteLine("-----------------------------");

        CompareSelectionSort.Int();
        CompareSelectionSort.Double();
        CompareSelectionSort.String();

        Console.WriteLine("-----------------------------");

        CompareInsertionSort.Int();
        CompareInsertionSort.Double();
        CompareInsertionSort.String();

        Console.WriteLine("-----------------------------");

        CompareRandomValues.QuickSort();
        CompareRandomValues.InsertionSort();
        CompareRandomValues.SelectionSort();

        Console.WriteLine("-----------------------------");

        CompareReverseSorted.QuickSort();
        CompareReverseSorted.InsertionSort();
        CompareReverseSorted.SelectionSort();

        Console.WriteLine("-----------------------------");

        CompareSortedValues.QuickSort();
        CompareSortedValues.InsertionSort();
        CompareSortedValues.SelectionSort();
    }
Exemplo n.º 30
0
        public static void Sort <T>(T[] a, Comparison <T> comparison)
        {
            var N = a.Length;

            for (var i = N / 2; i >= 1; --i)
            {
                Sink(a, i, N, comparison);
            }
            while (N > 0)
            {
                SortUtil.Exchange(a, Index(1), Index(N));
                Sink(a, 1, --N, comparison);
            }
        }
Exemplo n.º 31
0
 public static string MoveDown(string json, long topicId) {
     List<StickyTopic> topics = GetTopics( json );
     StickyTopic t = getById( topics, topicId );
     SortUtil<StickyTopic> s = new SortUtil<StickyTopic>( t, topics );
     s.MoveDown();
     return Json.ToStringList( s.GetOrderedList() );
 }
Exemplo n.º 32
0
 //-------------------------------------------------------------------------------------------------
 public static String MoveUp( String json, int topicId )
 {
     List<StickyTopic> topics = GetTopics( json );
     StickyTopic t = getById( topics, topicId );
     SortUtil<StickyTopic> s = new SortUtil<StickyTopic>( t, topics );
     s.MoveUp();
     return SimpleJsonString.ConvertList( s.GetOrderedList() );
 }