Exemplo n.º 1
0
        public static int[] Generate(int size, int MinRange, int MaxRange, ArraySort sortType)
        {
            int[] array = new int [size];
            Array.Sort(array);
            switch (sortType)
            {
            case ArraySort.Descending:
                Array.Reverse(array);
                break;

            case ArraySort.AShape:
            case ArraySort.VShape:
                if (sortType == ArraySort.AShape)
                {
                    Array.Reverse(array);
                }

                int[] shapeArray = new int[array.Length];
                int   j          = (int)Math.Floor((double)(array.Length - 1) / 2);

                for (int i = 0; i < array.Length; i++)
                {
                    shapeArray[j] = array[i];
                    j             = (i % 2 == 0) ? j + (i + 1) : j - (i + 1);
                }
                array = shapeArray;
                break;
            }
            return(array);
        }
Exemplo n.º 2
0
        public override SubQuery[] GetSubqueries(Session session)
        {
            OrderedHashSet <SubQuery> set = null;

            if (this._expression != null)
            {
                set = this._expression.CollectAllSubqueries(set);
            }
            for (int i = 0; i < this._arguments.Length; i++)
            {
                set = this._arguments[i].CollectAllSubqueries(set);
            }
            if ((set == null) || (set.Size() == 0))
            {
                return(SubQuery.EmptySubqueryArray);
            }
            SubQuery[] a = new SubQuery[set.Size()];
            set.ToArray(a);
            ArraySort.Sort <SubQuery>(a, 0, a.Length, a[0]);
            for (int j = 0; j < base.Subqueries.Length; j++)
            {
                a[j].PrepareTable(session);
            }
            return(a);
        }
Exemplo n.º 3
0
        public IList <IList <int> > ThreeSum(int[] nums)
        {
            IList <IList <int> > result = new List <IList <int> >();

            if (nums == null || nums.Length < 2)
            {
                return(result);
            }
            ArraySort.QuickSort(nums, 0, nums.Length - 1);

            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] > 0)
                {
                    return(result);
                }
                if (i > 0 && nums[i].Equals(nums[i - 1]))
                {
                    continue;
                }
                else
                {
                    int left  = i + 1;
                    int right = nums.Length - 1;
                    while (left < right)
                    {
                        int currentValue = nums[i] + nums[left] + nums[right];
                        if (currentValue.Equals(0))
                        {
                            result.Add(new List <int>
                            {
                                nums[i],
                                nums[left],
                                nums[right]
                            });
                            left++;
                            right--;
                            while (left < right && nums[left].Equals(nums[left - 1]))
                            {
                                left++;
                            }
                            while (left < right && nums[right].Equals(nums[right + 1]))
                            {
                                right--;
                            }
                        }
                        else if (currentValue > 0)
                        {
                            right--;
                        }
                        else
                        {
                            left++;
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 4
0
 public virtual void SortOrder()
 {
     if (this._queryExpression.OrderIndex != null)
     {
         this._mainIndex = this._queryExpression.OrderIndex;
         ArraySort.Sort <object[]>(this._table, 0, base.Size, this);
     }
     this.Reset();
 }
        public void QuickSortWithoutRecursionTest(int[] array)
        {
            ArraySort.QuickSortWithoutRecursion(array, 0, array.Length - 1);

            bool actual   = array.JudgeOrdered(true);
            bool excepted = true;

            Assert.Equal(excepted, actual);
        }
        public void SelectionSortTest(int[] array)
        {
            ArraySort.SelectionSort(array);

            bool actual   = array.JudgeOrdered(true);
            bool excepted = true;

            Assert.Equal(excepted, actual);
        }
Exemplo n.º 7
0
 public virtual void SortUnion(SortAndSlice sortAndSlice)
 {
     if (sortAndSlice.index != null)
     {
         this._mainIndex = sortAndSlice.index;
         ArraySort.Sort <object[]>(this._table, 0, base.Size, this);
         this.Reset();
     }
 }
Exemplo n.º 8
0
        public void MergeSort_ValidInputData_Test()
        {
            var result1 = ArraySort.MergeSort(_testArr1);

            CollectionAssert.AreEqual(_resultArr1, result1);

            var result2 = ArraySort.MergeSort(_testArr2);

            CollectionAssert.AreEqual(_resultArr2, result2);
        }
Exemplo n.º 9
0
        public void CanSortEmpty()
        {
            var items     = new int[] { };
            var threshold = 4;

            var count = ArraySort.MoveItemsLessThanThresholdToStart(items, threshold);

            IsArrayContentEqual(new int[] { }, items);
            Assert.AreEqual(0, count);
        }
Exemplo n.º 10
0
        public void CanSortNoItemsLessThanThreshold()
        {
            var items     = new[] { 6, 4, 5, 8 };
            var threshold = 4;

            var count = ArraySort.MoveItemsLessThanThresholdToStart(items, threshold);

            IsArrayContentEqual(new[] { 6, 4, 5, 8 }, items);
            Assert.AreEqual(0, count);
        }
Exemplo n.º 11
0
        public void CanSortAllItemsLessThanThreshold()
        {
            var items     = new[] { 3, 1, 2, 3 };
            var threshold = 4;

            var count = ArraySort.MoveItemsLessThanThresholdToStart(items, threshold);

            IsArrayContentEqual(new[] { 3, 1, 2, 3 }, items);
            Assert.AreEqual(4, count);
        }
Exemplo n.º 12
0
        public void CanSortWithNoAdjacentItemsLessThanThreshold()
        {
            var items     = new[] { 3, 6, 4, 2, 5, 3, 8 };
            var threshold = 4;

            var count = ArraySort.MoveItemsLessThanThresholdToStart(items, threshold);

            IsArrayContentEqual(new[] { 3, 2, 3, 6, 4, 5, 8 }, items);
            Assert.AreEqual(3, count);
        }
Exemplo n.º 13
0
        public void QuickSort_ValidInputData_Test()
        {
            var result1 = ArraySort.QuickSort(_testArr1, 0, _testArr1.Length - 1);

            CollectionAssert.AreEqual(_resultArr1, result1);

            var result2 = ArraySort.QuickSort(_testArr2, 0, _testArr2.Length - 1);

            CollectionAssert.AreEqual(_resultArr2, result2);
        }
Exemplo n.º 14
0
        public int ThreeSumClosest(int[] nums, int target)
        {
            if (nums == null)
            {
                return(0);
            }
            if (nums.Length == 1)
            {
                return(nums[0]);
            }
            if (nums.Length == 2)
            {
                return(nums[0] + nums[1]);
            }
            if (nums.Length == 3)
            {
                return(nums[0] + nums[1] + nums[2]);
            }
            ArraySort.QuickSort(nums, 0, nums.Length - 1);

            if (target > 0 && nums[0] > target)
            {
                return(nums[0] + nums[1] + nums[2]);
            }

            int sum  = 0;
            int diff = 2001;

            for (int i = 0; i < nums.Length - 2; i++)
            {
                int left = i + 1;
                while (left < nums.Length)
                {
                    int right = nums.Length - 1;
                    while (left < right)
                    {
                        int currentSum  = nums[i] + nums[left] + nums[right];
                        int currentDiff = currentSum - target;
                        if (currentDiff.Equals(0))
                        {
                            return(currentSum);
                        }
                        else if ((currentDiff * currentDiff) < (diff * diff))
                        {
                            sum  = currentSum;
                            diff = currentDiff;
                        }
                        right--;
                    }
                    left++;
                }
            }
            return(sum);
        }
Exemplo n.º 15
0
        public void FilterDigit_InputArray1217157_Return177()
        {
            // Arr
            int[] arr      = { 12, 17, 0, 7 };
            int[] expected = { 17, 7 };

            // Arc
            int[] actual = ArraySort.FilterDigit(arr, 7);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 16
0
        public void FilterDigit_Inputnegativenumbers_ReturnSuitableNumbers()
        {
            // Arr
            int[] arr      = { -12, -17, -20, 7 };
            int[] expected = { -12, -20 };

            // Arc
            int[] actual = ArraySort.FilterDigit(arr, 2);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void FilterDigit_InputValue0_NumbersWidth0()
        {
            // Arr
            int[] arr      = { 12, 0, 15, 105, 10456 };
            int[] expected = { 0, 105, 10456 };

            // Arc
            int[] actual = ArraySort.FilterDigit(arr, 0);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 18
0
        public void QuickSortTest()
        {
            var sorter = new ArraySort();

            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._fullyUnsortedEvenLengthArray.Clone(), 0, this._fullyUnsortedEvenLengthArray.Length - 1)));
            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._sortedEvenLengthArray.Clone(), 0, this._sortedEvenLengthArray.Length - 1)));
            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._unsortedEvenLengthArray.Clone(), 0, this._unsortedEvenLengthArray.Length - 1)));

            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._fullyUnsortedOddLengthArray.Clone(), 0, this._fullyUnsortedOddLengthArray.Length - 1)));
            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._sortedOddLengthArray.Clone(), 0, this._sortedOddLengthArray.Length - 1)));
            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.QuickSort((int[])this._unsortedOddLengthArray.Clone(), 0, this._unsortedOddLengthArray.Length - 1)));
        }
Exemplo n.º 19
0
        public void BubbleSortRecursiveTest()
        {
            var sorter = new ArraySort();

            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._fullyUnsortedEvenLengthArray.Clone(), this._fullyUnsortedEvenLengthArray.Length)));
            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._sortedEvenLengthArray.Clone(), this._sortedEvenLengthArray.Length)));
            Assert.AreEqual("0, 1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._unsortedEvenLengthArray.Clone(), this._unsortedEvenLengthArray.Length)));

            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._fullyUnsortedOddLengthArray.Clone(), this._fullyUnsortedOddLengthArray.Length)));
            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._sortedOddLengthArray.Clone(), this._sortedOddLengthArray.Length)));
            Assert.AreEqual("1, 2, 3, 4, 5, 6, 7", string.Join(", ", sorter.BubbleSortRecursive((int[])this._unsortedOddLengthArray.Clone(), this._unsortedOddLengthArray.Length)));
        }
Exemplo n.º 20
0
        public override void Compile(Session session, ISchemaObject parentObject)
        {
            using (Scanner scanner = new Scanner(this.statement))
            {
                ParserDQL rdql = new ParserDQL(session, scanner);
                rdql.Read();
                this.ViewSubQuery    = rdql.XreadViewSubquery(this);
                base.queryExpression = this.ViewSubQuery.queryExpression;
                if (base.GetColumnCount() == 0)
                {
                    if (this.ColumnNames == null)
                    {
                        this.ColumnNames = this.ViewSubQuery.queryExpression.GetResultColumnNames();
                    }
                    if (this.ColumnNames.Length != this.ViewSubQuery.queryExpression.GetColumnCount())
                    {
                        throw Error.GetError(0x15d9, this.GetName().StatementName);
                    }
                    TableUtil.SetColumnsInSchemaTable(this, this.ColumnNames, base.queryExpression.GetColumnTypes(), base.queryExpression.GetColumnNullability());
                }
                OrderedHashSet <SubQuery> set = OrderedHashSet <SubQuery> .AddAll(OrderedHashSet <SubQuery> .Add(base.queryExpression.GetSubqueries(), this.ViewSubQuery), this.ViewSubQuery.GetExtraSubqueries());

                this.ViewSubqueries = new SubQuery[set.Size()];
                set.ToArray(this.ViewSubqueries);
                ArraySort.Sort <SubQuery>(this.ViewSubqueries, 0, this.ViewSubqueries.Length, this.ViewSubqueries[0]);
                foreach (SubQuery query in this.ViewSubqueries)
                {
                    if (query.ParentView == null)
                    {
                        query.ParentView = this;
                    }
                    query.PrepareTable(session);
                }
                this.ViewSubQuery.GetTable().view       = this;
                this.ViewSubQuery.GetTable().ColumnList = base.ColumnList;
                this._schemaObjectNames = rdql.compileContext.GetSchemaObjectNames();
                this._baseTable         = base.queryExpression.GetBaseTable();
            }
            if (this._baseTable != null)
            {
                switch (this._check)
                {
                case 0:
                case 2:
                    return;

                case 1:
                    base.queryExpression.GetCheckCondition();
                    return;
                }
                throw Error.RuntimeError(0xc9, "View");
            }
        }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            int[]    firstArray  = new int[] { 12, 6, -122, 5, 890, -3, 24, 89, 4, -76, 0 };
            string[] secondArray = new string[] { "one", "two", "three", "four", "five", "six" };

            ArraySort <int> .Sort(firstArray, new IntComparer());

            ArraySort <string> .Sort(secondArray, new StringComparer());

            Display(firstArray);
            Display(secondArray);
            Console.ReadKey();
        }
Exemplo n.º 22
0
        public void NumberFinderByString_171017_177()
        {
            // Arr
            int[] array = new int[] { 17, 10, 1, 7 };
            int   digit = 7;

            int[] expected = new int[] { 17, 7 };

            // Act
            int[] actual = ArraySort.FilterDigit(array, digit, true);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 23
0
        public virtual IRowIterator FindFirstRow(object[] data)
        {
            int position = ArraySort.SearchFirst <object[]>(this._table, 0, base.Size, data, this);

            if (position < 0)
            {
                position = base.Size;
            }
            else
            {
                position--;
            }
            return(new DataIterator(position, this));
        }
Exemplo n.º 24
0
        public void NumberFinderByString_123456101636_61636()
        {
            // Arr
            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 10, 16, 36 };
            int   digit = 6;

            int[] expected = new int[] { 6, 16, 36 };

            // Act
            int[] actual = ArraySort.FilterDigit(array, digit, true);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 25
0
 private void SaveRows(int count)
 {
     lock (this)
     {
         if (count != 0)
         {
             this._rowComparator.SetType(1);
             this._sortTimer.Start();
             ArraySort.Sort <ICachedObject>(this._rowTable, 0, count, this._rowComparator);
             this._sortTimer.Stop();
             this._saveAllTimer.Start();
             this.dataFileCache.SaveRows(this._rowTable, 0, count);
             this._saveRowCount += count;
             this._saveAllTimer.Stop();
         }
     }
 }
Exemplo n.º 26
0
        public T[] SubArray(int fromPosition, int numberOfElements, ArraySort order = ArraySort.Unsorted)
        {
            if (fromPosition < 0 || fromPosition >= this.Array.Length || numberOfElements <= 0)
            {
                return(new T[0]);
            }

            int index = 0;

            T[] singleArray = new T[this.Array.GetLength(0) * this.Array.GetLength(1)];

            for (int i = 0; i < this.Array.GetLength(0); i++)
            {
                for (int j = 0; j < this.Array.GetLength(1); j++)
                {
                    singleArray[index] = this.Array[i, j];
                    index++;
                }
            }

            if (fromPosition + numberOfElements > singleArray.Length)
            {
                T[] result = new T[singleArray.Length - fromPosition];

                for (int i = fromPosition, j = 0; i < singleArray.Length; i++, j++)
                {
                    result[j] = singleArray[i];
                }

                result = SortArray(result, order);

                return(result);
            }

            T[] single = new T[numberOfElements];

            for (int i = fromPosition, j = 0; i < singleArray.Length && j < numberOfElements; i++, j++)
            {
                single[j] = singleArray[i];
            }

            single = SortArray(single, order);

            return(single);
        }
Exemplo n.º 27
0
 public virtual void Union(RowSetNavigatorData other)
 {
     this.RemoveDuplicates();
     other.RemoveDuplicates();
     while (other.HasNext())
     {
         object[] next = other.GetNext();
         int      num  = ArraySort.SearchFirst <object[]>(this._table, 0, base.Size, next, this);
         if (num < 0)
         {
             num             = -num - 1;
             base.CurrentPos = num;
             this.Insert(next);
         }
     }
     other.Close();
     this.Reset();
 }
        public void SortByMaxAbsRowElement_AscendentOrder_ChangedInitialArray()
        {
            int[][] expected = new int[5][] { new int[] { 15, 5, 7, 10, -15, 7 }, new int[] { 8, 4, 7, 10, 9, 5, 20, }, new int[] { 1, 30, 7, 3 - 30 }, null, null };

            ArraySort.Sort(initial, (x, y) =>
            {
                if (x == null && y == null)
                {
                    return(0);
                }
                if (x == null)
                {
                    return(-1);
                }
                if (y == null)
                {
                    return(1);
                }
                if (x.Select(x1 => Math.Abs(x1)).ToArray().Max() < y.Select(y1 => Math.Abs(y1)).ToArray().Max())
                {
                    return(1);
                }
                else if (x.Select(x1 => Math.Abs(x1)).ToArray().Max() > y.Select(y1 => Math.Abs(y1)).ToArray().Max())
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });

            for (int i = 0; i < initial.Length; i++)
            {
                CollectionAssert.AreEqual(initial[i], expected[i]);
            }

            ArraySort.Sort(initial, new AbsCondition());

            for (int i = 0; i < initial.Length; i++)
            {
                CollectionAssert.AreEqual(initial[i], expected[i]);
            }
        }
Exemplo n.º 29
0
        public virtual SubQuery[] GetSubqueries(Session session)
        {
            OrderedHashSet <SubQuery> first = null;

            for (int i = 0; i < this.TargetRangeVariables.Length; i++)
            {
                if (this.TargetRangeVariables[i] != null)
                {
                    OrderedHashSet <SubQuery> subqueries = this.TargetRangeVariables[i].GetSubqueries();
                    first = OrderedHashSet <SubQuery> .AddAll(first, subqueries);
                }
            }
            for (int j = 0; j < this.UpdateExpressions.Length; j++)
            {
                first = this.UpdateExpressions[j].CollectAllSubqueries(first);
            }
            if (this.InsertExpression != null)
            {
                first = this.InsertExpression.CollectAllSubqueries(first);
            }
            if (this.condition != null)
            {
                first = this.condition.CollectAllSubqueries(first);
            }
            if (this.queryExpression != null)
            {
                OrderedHashSet <SubQuery> subqueries = this.queryExpression.GetSubqueries();
                first = OrderedHashSet <SubQuery> .AddAll(first, subqueries);
            }
            if ((first == null) || (first.Size() == 0))
            {
                return(SubQuery.EmptySubqueryArray);
            }
            SubQuery[] a = new SubQuery[first.Size()];
            first.ToArray(a);
            ArraySort.Sort <SubQuery>(a, 0, a.Length, a[0]);
            for (int k = 0; k < a.Length; k++)
            {
                a[k].PrepareTable(session);
            }
            return(a);
        }
Exemplo n.º 30
0
        private T[] SortArray(T[] input, ArraySort order)
        {
            switch (order)
            {
            case ArraySort.Ascending:
                System.Array.Sort(input);
                break;

            case ArraySort.Descending:
                System.Array.Sort(input);
                System.Array.Reverse(input);
                break;

            case ArraySort.Unsorted:
            default:
                break;
            }

            return(input);
        }
 public void SortArray_Test(int[][] sortedArray, IComparer<int[]> comparator, ArraySort sort) {
     sort.Sort(m_Array, comparator);
     CollectionAssert.AreEqual(m_Array, sortedArray);
 }