简体   繁体   English

堆排序未对数组的最后一个和第一个元素进行排序

[英]Heap-Sort not sorting the last and first element of the array

I am trying to implement heap-sort in java. 我正在尝试在Java中实现堆排序。 I searched the internet for some Pseudo-code and I looked at these three sites: 我在互联网上搜索了一些伪代码,然后查看了这三个站点:

http://www.cc.gatech.edu/classes/cs3158_98_fall/heapsort.html http://www.cc.gatech.edu/classes/cs3158_98_fall/heapsort.html

http://www.algorithmist.com/index.php/Heap_sort http://www.algorithmist.com/index.php/Heap_sort

http://thevigilantzephyr.blogspot.ie/2011/11/heap-sort-algorithm-and-pseudo-code.html http://thevigilantzephyr.blogspot.ie/2011/11/heap-sort-algorithm-and-pseudo-code.html

Now I have implemented a solution in java but when I run it it does not seem to sort the first element and the last element of the array. 现在,我已经在Java中实现了一个解决方案,但是当我运行它时,似乎并没有对数组的第一个元素和最后一个元素进行排序。

Here is my code: 这是我的代码:

package heapSort;

import java.util.Comparator;

public class heapSort
{
    static int heapSize;

    public static <type> void sort(type[] array, Comparator<type> comp)
    {

        buildHeap(array, comp);
        for (int index = array.length - 1; index > 1; index--)
        {
            swap(array, 1, index);
            heapSize--;
            heap(array, 1, comp);
        }
    }

    private static <type> void buildHeap(type[] array, Comparator<type> comp)
    {
        heapSize = array.length - 1;
        for (int index = (int) Math.floor((array.length - 1) / 2); index > 1; index--)
        {
            heap(array, index, comp);
        }
        return;
    }

    private static <type> void heap(type[] array, int index, Comparator<type> comp)
    {
        int left = 2 * index;
        int right = 2 * index + 1;
        int largest;

        if (left <= heapSize && comp.compare(array[left], array[index]) > 0)
        {
            largest = left;
        }
        else
        {
            largest = index;
        }

        if (right <= heapSize && comp.compare(array[right], array[largest]) > 0)
        {
            largest = right;
        }

        if (largest != index)
        {
            swap(array, index, largest);
            heap(array, largest, comp);
        }
    }

    private static <type> void swap(type[] array, int index1, int index2)
    {
        type temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
        return;
    }

}

Here is my tester class: 这是我的测试人员课程:

package heapSort;

import java.util.Arrays;
import java.util.Comparator;

public class quicksortTesterinplcae
{
    public static void main(String[] args)
    {
        /**
         * String comparator alphabetical
         */
        Comparator<String> comp = new Comparator<String>()
        {
            public int compare(String arg0, String arg1)
            {
                return arg0.compareTo(arg1);
            }
        };

        /**
         * Integer comparator ascending
         */
        Comparator<Integer> compint = new Comparator<Integer>()
        {
            public int compare(Integer o1, Integer o2)
            {
                return o1.compareTo(o2);
            }
        };

        /**
         * Two test data sets
         */
        String[] test = "hello world the cat sat on the bloody mat".split("\\s");
        Integer[] testint =
        { 4, 2, 3, 4, 8, 9, 1, 2, 3, 7, 9, 8, 4, 2, 33, 22, 44, 66, 77, 88, 9, 87, 5, 3, 22 };

        /**
         * Print the unsorted data
         */
        System.out.println(Arrays.toString(test));
        System.out.println(Arrays.toString(testint));

        /**
         * Sort the two data sets
         */
        heapSort.sort(test, comp);
        heapSort.sort(testint, compint);

        /**
         * Print the sorted sets
         */
        System.out.println(Arrays.toString(test));
        System.out.println(Arrays.toString(testint));

        System.exit(0);

    }
}

But when I run the code I get the following: 但是,当我运行代码时,我得到以下信息:

[hello, world, the, cat, sat, on, the, bloody, mat] [你好,世界,猫,坐在,上面,血腥,垫子上]

[4, 2, 3, 4, 8, 9, 1, 2, 3, 7, 9, 8, 4, 2, 33, 22, 44, 66, 77, 88, 9, 87, 5, 3, 22] [4、2、3、4、8、9、1、2、3、7、9、8、4、2、33、22、44、66、77、88、9、87、5、3、22 ]

[hello, bloody, cat, mat, on, sat, the, the, world] [你好,血腥的,猫,垫子,在,坐着,那个,世界上]

[4, 1, 2, 2, 3, 3, 3, 4, 4, 5, 7, 8, 8, 9, 9, 9, 22, 22, 33, 44, 66, 77, 87, 88, 2] [4、1、2、2、3、3、3、4、4、5、7、8、8、9、9、9、22、22、33、44、66、77、87、88、2 ]

The code you can use for Heap sort could be:- 您可以用于堆排序的代码可能是:-

class heapSort {
static int heapSize;

public static <type> void sort(type[] arr, Comparator<type> comp) {

    heapSize = arr.length;

    // Build heap (rearrange array)
    for (int i = heapSize / 2 - 1; i >= 0; i--)
        heap(arr,heapSize, i, comp);

    // One by one extract an element from heap
    for (int i = heapSize - 1; i >= 0; i--) {
        // Move current root to end
        type temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;

        // call max heapify on the reduced heap
        heap(arr, i,0, comp);
    }
}

private static <type> void heap(type[] arr,int n, int i, Comparator<type> comp) {
    int largest = i;  // Initialize largest as root
    int l = 2 * i + 1;  // left = 2*i + 1
    int r = 2 * i + 2;  // right = 2*i + 2

    // If left child is larger than root
    if (l < n && comp.compare(arr[l], arr[largest])>0)
        largest = l;

    // If right child is larger than largest so far
    if (r < n && comp.compare(arr[r], arr[largest])>0)
        largest = r;

    // If largest is not root
    if (largest != i) {
        type swap = arr[i];
        arr[i] = arr[largest];
        arr[largest] = swap;

        // Recursively heapify the affected sub-tree
        heap(arr,n, largest, comp);
    }
}

private static <type> void swap(type[] array, int index1, int index2) {
    type temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
    return;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM