使用堆排序对数组进行排序

时间:2017-03-15 05:18:38

标签: java algorithm heapsort max-heap

我正在使用算法的中期审核,我尝试用Java实现所有伪代码,以便更好地理解算法。但是在堆排序部分,我的代码出了问题。我的输入数组是

{10,16,4,10,14,7,9,3,2,8,1}

并且第一个元素仅表示我想要排序的元素数。换句话说,需要排序的元素从索引1开始。

我的构建max-heap的输出是: 16 14 10 8 7 9 3 2 4 1

我的堆排序输出是: 1 3 2 4 7 8 9 10 14 16

似乎我的build-max-heap部分运行良好,但我也找不到堆排序部分中的错误。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>xptraining</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/rest.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

2 个答案:

答案 0 :(得分:0)

检查以下代码。

public class HeapSort
  {
   public void sort(int arr[])
    {
     int n = arr.length;

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

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

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

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
    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 && arr[l] > arr[largest])
        largest = l;

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

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

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

/* A utility function to print array of size n */
static void printArray(int arr[])
{
    int n = arr.length;
    for (int i=0; i<n; ++i)
        System.out.print(arr[i]+" ");
    System.out.println();
}

// Driver program
public static void main(String args[])
{
    int arr[] = {12, 11, 13, 5, 6, 7};
    int n = arr.length;

    HeapSort ob = new HeapSort();
    ob.sort(arr);

    System.out.println("Sorted array is");
    printArray(arr);
 }
}

答案 1 :(得分:0)

您的代码中有2个错误:

1。 heapsort的for循环从last element转到first element,所以

for (int i = n; i >= 2; i--){

应该是:

for (int i = n; i >= 1; i--){

因为索引从 0 开始。

<强> 2 即可。执行exchange(A, 1, i)后,正确的呼叫应为:

maxHeapify(A, 1, i)