Ejemplo n.º 1
0
        /**
         * heap sort using Comparable, first converts array into a valid max heap by iterating over the maxHeapify method.
         * Then puts root into leaf position, put leaf into root, let it sink down in an array of size n-1 so the end of the array is finalized.
         * Iterate over it until every node has been taken from the root position and put into the finalized portion of the array at which point the array is sorted in increasing order.
         * @param x - the input array containing jobs that need to be sorted.
         * @param n - the size of the input array
         */
        public static void sortHeap(int[] x, int n)
        {
            int endParent = (n / 2) - 1;

            //Keep updating the last node to have a parent which has not been heapified yet
            while (endParent > -1)
            {
                sinkHeapify(x, endParent, n);
                endParent--;
            }

            int temp = 0;

            int N = n;

            //Put root into leaf position, put leaf into root, let it sink down in an array of size n-1 so the end of the array is finalized.
            //Iterate over it until every node has been taken from the root position and put into the finalized portion of the array at which
            //point the array is sorted in increasing order.
            while (N > 0)
            {
                N--;
                temp = x[0];
                x[0] = x[N];
                x[N] = temp;
                sinkHeapify(x, 0, N);
            }

            if (Compare.compareTo(x[1], x[0]) < 0)
            {
                Compare.swap(x, 0, 1);
            }
        }
Ejemplo n.º 2
0
        /**
         * In place shellsort, does not use extra memory, iterates along specified interval
         * @param low - the left most index of the left sub array
         * @param high - the right most index of the right sub array
         * @param mid - the right most index of the left sub array (The index value where the two subarrays were partitioned from)
         */
        public static void shellSort(int[] a, int N)
        {
            int h = 1;

            while (h < N / 3)
            {
                h = 3 * h + 1;
            }
            while (h >= 1)
            {
                for (int i = h; i < N; i++)
                {
                    for (int j = i; j >= h && Compare.compareTo(a[j], a[j - h]) < 0; j -= h)
                    {
                        Compare.swap(a, j, j - h);
                    }
                }
                h = h / 3;
            }
        }
Ejemplo n.º 3
0
        /**
         * Using the concept of the sink function this function is used to take an array and turn it into a heap recursively through each subtree.
         * The method is called iteratively from the last node in the array that has children all the way up to the root node.
         * To avoid creating a new array of size N+1 in order to shift the root to index 1, the left and right children are just offset by one in their calculation.
         * @param x - the input array containing jobs that need to be sorted.
         * @param index - the index of the root node where everything below it will be recursively reconfigured into a valid MaxHeap.
         * @param N - the size of the input array
         */
        public static void sinkHeapify(int[] x, int index, int N)
        {
            int left  = index * 2 + 1;
            int right = index * 2 + 2;

            if (index < 0 || left > N - 1 || right > N - 1)
            {
                return;
            }

            if (Compare.compareTo(x[left], x[right]) < 0 && Compare.compareTo(x[index], x[right]) < 0)
            {
                Compare.swap(x, right, index);
                sinkHeapify(x, right, N);
            }
            else if (Compare.compareTo(x[index], x[left]) < 0)
            {
                Compare.swap(x, left, index);
                sinkHeapify(x, left, N);
            }
        }
Ejemplo n.º 4
0
        /**
         * reassemble two subarraysback together by passing in the left most index of the left subarray and the rightmost index of the right subarray in
         * the main array. Since the subarrays will already be sorted since we are building back up from single elements, the subarrays are merged back
         * together by comparing the first index value of each subarray and moving the smaller one into the next open index in the main array and incrementing
         * the index of that subarray by one. Once all the elements of one of the subarrays have been iterated through, the remaining elements of the other
         * subarray are simply added to the remaining slots of the main array in the order that they are in.
         * @param x - the input array containing jobs that need to be sorted.
         * @param low - the left most index of the left sub array
         * @param high - the right most index of the right sub array
         * @param mid - the right most index of the left sub array (The index value where the two subarrays were partitioned from)
         */
        private static void reassemble(int[] x, int low, int high, int mid)
        {
            //When reassembling the subarrays back together, take the right subarray and left subarray, take the lowest initial value from the two and put it back into the original array in its appropriate spot, then once one array has been fully placed into the original array, put all the remaining elements from the other array back into the origina array in sequence

            int[] right = new int[high - mid];
            int[] left  = new int[1 + mid - low];

            int countR = 0;
            int countL = 0;
            int z      = 0;

            int c = 0;

            for (int i = low; i <= mid; i++)
            {
                left[c] = x[c + low];
                c++;
            }

            c = 0;

            for (int i = mid + 1; i <= high; i++)
            {
                right[c] = x[c + mid + 1];
                c++;
            }


            for (z = low; z <= high && countR < high - mid && countL < 1 + mid - low; z++)
            {
                if (Compare.compareTo(right[countR], left[countL]) < 0)
                {
                    x[z] = right[countR];
                    countR++;
                }
                else
                {
                    x[z] = left[countL];
                    countL++;
                }
            }


            if (countR != right.Length)
            {
                while (countR < right.Length)
                {
                    x[z] = right[countR];
                    countR++;
                    z++;
                }
            }
            else
            {
                while (countL < left.Length)
                {
                    x[z] = left[countL];
                    countL++;
                    z++;
                }
            }
        }