コード例 #1
0
        /// <summary>
        /// uses quick sort algorithm.
        /// </summary>
        /// <param name="data">data array</param>
        /// <param name="left">start index</param>
        /// <param name="right">end index</param>
        /// <param name="ascending">ascending or descending</param>
        public void QuickSort(ref SortInfo[] data, int left, int right, bool ascending)
        {
            int   leftIndex  = left;
            int   rightIndex = right;
            float pivot      = data[(left + right) / 2].depth;

            SortInfo temp = new SortInfo();

            while (leftIndex <= rightIndex)
            {
                //  search
                if (ascending)
                {
                    for (; data[leftIndex].depth < pivot; leftIndex++)
                    {
                        ;
                    }
                    for (; data[rightIndex].depth > pivot; rightIndex--)
                    {
                        ;
                    }
                }
                else
                {
                    for (; data[leftIndex].depth > pivot; leftIndex++)
                    {
                        ;
                    }
                    for (; data[rightIndex].depth < pivot; rightIndex--)
                    {
                        ;
                    }
                }

                //  swap two values
                if (leftIndex <= rightIndex)
                {
                    data[leftIndex].CopyTo(ref temp);
                    data[rightIndex].CopyTo(ref data[leftIndex]);
                    temp.CopyTo(ref data[rightIndex]);

                    leftIndex++;
                    rightIndex--;
                }
            }

            //  retry sorting
            if (rightIndex > left)
            {
                QuickSort(ref data, left, rightIndex, ascending);
            }

            //  retry sorting
            if (leftIndex < right)
            {
                QuickSort(ref data, leftIndex, right, ascending);
            }
        }
コード例 #2
0
 public void CopyTo(ref SortInfo target)
 {
     target.node  = this.node;
     target.depth = this.depth;
 }