예제 #1
0
        /// <summary>
        /// Selects a pivot using the SelectPivot method and arranges the input such that left side elements are lesser than pivot
        /// and right side elements are greater than pivot
        /// </summary>
        /// <param name="searchSample"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public int Partition(int[] searchSample, int min, int max)
        {
            int pos = SelectPivot(searchSample, min, max);
            int x   = searchSample[pos];
            int i   = min - 1;

            CommonClass.Swap(ref searchSample[pos], ref searchSample[max]);
            for (int j = min; j < max; j++)
            {
                if (searchSample[j] <= x)
                {
                    i++;
                    if (i != j)
                    {
                        CommonClass.Swap(ref searchSample[i], ref searchSample[j]);
                    }
                }
                CommonClass.IncreaseCount();
            }
            i++;
            if (i != max)
            {
                CommonClass.Swap(ref searchSample[i], ref searchSample[max]);
            }
            return(i);
        }
예제 #2
0
        /// <summary>
        /// Deletes the first element from the heap
        /// </summary>
        /// <param name="searchSample"></param>
        /// <returns></returns>
        public int Delete(ref int[] searchSample)
        {
            int max      = searchSample[0];
            int heapSize = searchSample.Length;

            CommonClass.Swap(ref searchSample[0], ref searchSample[heapSize - 1]);
            heapSize--;
            Array.Resize(ref searchSample, heapSize);
            Heapify(searchSample, 0);
            return(max);
        }
예제 #3
0
        public int  CommonPartition(int[] searchSample, int min, int max, int pos)
        {
            int x = searchSample[pos];
            int i = min;

            CommonClass.Swap(ref searchSample[pos], ref searchSample[max]);
            for (int j = min; j < max; j++)
            {
                if (searchSample[j] < x)
                {
                    CommonClass.Swap(ref searchSample[i], ref searchSample[j]);
                    i++;
                }
            }
            CommonClass.Swap(ref searchSample[i], ref searchSample[max]);
            return(i);
        }
예제 #4
0
        /// <summary>
        /// Finds the i largest elements in an array using linear search
        /// </summary>
        /// <param name="searchSample"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public int[] LinearSearch(int[] searchSample, int size)
        {
            int[] result = new int[size];

            for (int i = 0; i < size; i++)
            {
                int max = searchSample[i];
                int pos = i;
                for (int j = i + 1; j < searchSample.Length; j++)
                {
                    CommonClass.IncreaseCount();
                    if (searchSample[j] > max)
                    {
                        max = searchSample[j];
                        pos = j;
                    }
                }
                CommonClass.Swap(ref searchSample[i], ref searchSample[pos]);
                result[i] = max;
            }
            return(result);
        }
예제 #5
0
        /// <summary>
        /// Creates the heap around the index i
        /// </summary>
        /// <param name="searchSample"></param>
        /// <param name="index"></param>
        public void Heapify(int[] searchSample, int index)
        {
            int heapsize = searchSample.Length;
            int left;

            if (index == 0)
            {
                left = 1;
            }
            else
            {
                left = 2 * index + 1;
            }
            int right   = left + 1;
            int largest = index;

            if (left < heapsize && searchSample[left] > searchSample[largest])
            {
                largest = left;
            }
            CommonClass.IncreaseCount(); CommonClass.IncreaseCount();

            if (right < heapsize && searchSample[right] > searchSample[largest])
            {
                largest = right;
            }

            CommonClass.IncreaseCount(); CommonClass.IncreaseCount();

            if (largest != index)
            {
                CommonClass.Swap(ref searchSample[index], ref searchSample[largest]);
                Heapify(searchSample, largest);
            }
            CommonClass.IncreaseCount();
        }