Esempio n. 1
0
 private void TestQuickSortLoop(int len, int rep)
 {
     try {
         m_stopwatch.Stop();
         m_stopwatch.Reset();
         for (int r = 0; r < rep; r++)
         {
             double[] p = ILMath.rand(1, len).m_data;
             m_stopwatch.Start();
             ILQuickSort.QuickSortAscSolid_IT(p, 0, len - 1, 1);
             m_stopwatch.Stop();
             if (!ILMath.IsSorted(p, false))
             {
                 throw new Exception("unsorted values detected (asc). Size: " + p.Length);
             }
             // descending
             ILQuickSort.QuickSortDescSolid_IT(p, 0, len - 1, 1);
             if (!ILMath.IsSorted(p, true))
             {
                 throw new Exception("unsorted values detected (desc). Size: " + p.Length);
             }
         }
         Success(" elapsed: " + m_stopwatch.ElapsedMilliseconds + " ms");
     } catch (Exception e) {
         Error(0, e.Message);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// sort data in A along dimension 'dim'
        /// </summary>
        /// <param name="A">input array, n-dimensional</param>
        /// <param name="descending">Specifies the direction of sorting</param>
        /// <param name="dim">dimension to sort along</param>
        /// <param name="Indices">output parameter: returns permutation matrix also</param>
        /// <returns>sorted array of the same size as A</returns>
        /// <remarks><para>The data in A will be sorted using the quick sort algorithm. Data
        /// along the dimension <paramref name="dim"/> will therefore get sorted independently from data
        /// in the next row/column.</para>
        /// <para>This overload also returns an array 'Indices' which will hold the indices into the original
        /// elements <b>after sorting</b>. Elements of 'Indices' are of type double.</para>
        /// <example><code>ILArray&lt;double&gt; A = ILMath.rand(1,5);
        /// //A:
        /// // 0.4324  0.9231  0.1231  0.1423  0.2991
        /// ILArray&lt;double&gt; I;
        /// ILArray&lt;double&gt; R = ILMath.sort(A, out I, 1, false);
        /// //R:
        /// // 0.1231  0.1423  0.2991  0.4324  0.9231
        /// //I:
        /// // 2  3  4  0  1
        /// </code>
        /// </example></remarks>
        public static ILArray <byte> sort(ILArray <byte> A, out ILArray <double> Indices, int dim, bool descending)
        {
            if (object.Equals(A, null))
            {
                throw new Exception("sort: parameter A must not be null");
            }
            if (dim < 0 || dim >= A.Dimensions.NumberOfDimensions)
            {
                throw new ILArgumentException("sort: invalid dimension argument");
            }
            // early exit: scalar/ empty
            Indices = ILMath.counter(0.0, 1.0, A.Dimensions.ToIntArray());
            if (A.IsEmpty || A.IsScalar)
            {
                return(A.C);
            }
            ILArray <byte> ret      = A.C;
            int            inc      = ret.Dimensions.SequentialIndexDistance(dim);
            int            dimLen   = A.Dimensions[dim];
            int            maxRuns  = A.Dimensions.NumberOfElements / dimLen;
            int            posInArr = 0;

            if (descending)
            {
                for (int c = 0; c < maxRuns; c++)
                {
                    if (posInArr >= A.Dimensions.NumberOfElements)
                    {
                        posInArr -= (A.Dimensions.NumberOfElements - 1);
                    }
                    ILQuickSort.QuickSortDescSolidIDX(ret.m_data, Indices.m_data, posInArr, posInArr + (dimLen - 1) * inc, inc);
                    posInArr += dimLen * inc;
                }
            }
            else
            {
                // ascending
                for (int c = 0; c < maxRuns; c++)
                {
                    if (posInArr >= A.Dimensions.NumberOfElements)
                    {
                        posInArr -= (A.Dimensions.NumberOfElements - 1);
                    }
                    ILQuickSort.QuickSortAscSolidIDX(ret.m_data, Indices.m_data, posInArr, posInArr + (dimLen - 1) * inc, inc);
                    posInArr += dimLen * inc;
                }
            }
            return(ret);
        }
Esempio n. 3
0
        private void TestQuickSort(double[] p)
        {
            int errCode = 0;

            try {
                m_stopwatch.Reset();
                ILQuickSort.QuickSortAscSolid_IT(p, 0, p.Length - 1, 1);
                m_stopwatch.Stop();
                if (!ILMath.IsSorted(p, false))
                {
                    throw new Exception("unsorted values detected. Size: " + p.Length);
                }
                Success();
            } catch (Exception e) {
                Error(errCode, e.Message);
            }
        }