Exemplo n.º 1
0
 public void Sort(int start, int end, com.epl.geometry.AttributeStreamOfInt32.IntComparator compare)
 {
     if (end - start < 10)
     {
         Insertionsort(start, end, compare);
     }
     else
     {
         Quicksort(start, end - 1, compare, new com.epl.geometry.AttributeStreamOfInt32.RandomSeed());
     }
 }
Exemplo n.º 2
0
        internal void Quicksort(int leftIn, int rightIn, com.epl.geometry.AttributeStreamOfInt32.IntComparator compare, com.epl.geometry.AttributeStreamOfInt32.RandomSeed seed)
        {
            if (leftIn >= rightIn)
            {
                return;
            }
            int left  = leftIn;
            int right = rightIn;

            while (true)
            {
                // tail recursion loop
                if (right - left < 9)
                {
                    Insertionsort(left, right + 1, compare);
                    return;
                }
                // Select random index for the pivot
                seed.random = com.epl.geometry.NumberUtils.NextRand(seed.random);
                long nom        = ((long)(right - left)) * seed.random;
                int  pivotIndex = (int)(nom / com.epl.geometry.NumberUtils.IntMax()) + left;
                // Get the pivot value
                int pivotValue = m_buffer[pivotIndex];
                // Start partition
                // Move pivot to the right
                Swap(pivotIndex, right);
                int storeIndex = left;
                for (int i = left; i < right; i++)
                {
                    int elm = m_buffer[i];
                    if (compare.Compare(elm, pivotValue) <= 0)
                    {
                        Swap(storeIndex, i);
                        storeIndex = storeIndex + 1;
                    }
                }
                // Move pivot to its final place
                Swap(storeIndex, right);
                // End partition
                // Shorter part is regular recursion
                // Longer part is tail recursion
                if (storeIndex - left < right - storeIndex)
                {
                    Quicksort(left, storeIndex - 1, compare, seed);
                    left = storeIndex + 1;
                }
                else
                {
                    Quicksort(storeIndex + 1, right, compare, seed);
                    right = storeIndex - 1;
                }
            }
        }
Exemplo n.º 3
0
 internal void Insertionsort(int start, int end, com.epl.geometry.AttributeStreamOfInt32.IntComparator compare)
 {
     for (int j = start; j < end; j++)
     {
         // insertion sort
         int key = m_buffer[j];
         int i   = j - 1;
         while (i >= start && compare.Compare(m_buffer[i], key) > 0)
         {
             m_buffer[i + 1] = m_buffer[i];
             i--;
         }
         m_buffer[i + 1] = key;
     }
 }