簡體   English   中英

堆排序未對數組的最后一個和第一個元素進行排序

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

我正在嘗試在Java中實現堆排序。 我在互聯網上搜索了一些偽代碼,然后查看了這三個站點:

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

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

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

現在,我已經在Java中實現了一個解決方案,但是當我運行它時,似乎並沒有對數組的第一個元素和最后一個元素進行排序。

這是我的代碼:

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;
    }

}

這是我的測試人員課程:

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);

    }
}

但是,當我運行代碼時,我得到以下信息:

[你好,世界,貓,坐在,上面,血腥,墊子上]

[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、1、2、2、3、3、3、4、4、5、7、8、8、9、9、9、22、22、33、44、66、77、87、88、2 ]

您可以用於堆排序的代碼可能是:-

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