简体   繁体   English

索引从1到n的HeapSort算法,实际代码必须从0到n-1

[英]HeapSort Algorithm Indexed 1 through n, and actual code has to be from 0 to n-1

I'm kind of new to algorithms and wanted to implement heap sort algorithm. 我对算法有点陌生,想实现堆排序算法。 The algorithm is given as follows: 该算法如下:

Parent(i) return Math.floor(i/2) 父级(i)返回Math.floor(i / 2)

Left(i) return 2i Left(i)返回2i

Right(i) return 2i+1 向右(i)返回2i + 1

Then there is HEAPIFY method that restores the heep property. 然后有HEAPIFY方法可还原heep属性。 Algorithm is as follows: 算法如下:

HEAPIFY(A, i)
 l = Left(i)
 r = Right(i)
 if (l <= heap-size[A] and A[l] > A[i]
  then largest = l
 else largest = i
 if r <= heap-size[A] and A[r] > A[largest]
  then largest = r
 if largest != i
  then exchange A[i] <-> A[largest]
       HEAPIFY(A, largest)

My Code that implements this method is: 我实现此方法的代码是:

public static void HEAPIFY(int[] A, int i) {
    int l = LEFT(i);
    int r = RIGHT(i);
    int largest = 0;
    if (l < A.length && A[l] > A[i]) {
        largest = l;
    } else {
        largest = i;
    }

    if (r < A.length && A[r] > A[largest]) {
        largest = r;
    }

    if (largest != i) {
        int temp = A[i];
        A[i] = A[largest];
        A[largest] = temp;
        HEAPIFY(A, largest);
    }
}

Now My question is in the book algorithm is shown by drawing the tree of heap and array so for example array is: [16,14,10,8,7,9,3,2,4,1] and for the tree and also for array it is indexed starting from 1 to n, so Array[1] = 16 and in coding Array[0] = 16. Now i can not adjust the heapify method to start either from index 1 and go up to 1 or somehow make it start from 0 and let the heap be indexed from 0 to n-1. 现在我的问题在书中,算法是通过绘制堆树和数组树来显示的,例如,数组为:[16,14,10,8,7,9,3,2,4,1]并为树和同样对于数组,它的索引是从1到n,因此Array [1] = 16并在编码Array [0] = 16时。现在,我无法调整heapify方法以从索引1开始并升至1或以某种方式使它从0开始,并使堆从0索引到n-1。

Sorry if its kind of confusing i'm still confused but i would really appreciate some help. 对不起,如果仍然感到困惑,我仍然感到困惑,但是我将非常感谢您的帮助。

Thank you guys 感谢大伙们

Now HEAPIFY works and the following code is code to build the heap: 现在,HEAPIFY可以工作了,下面的代码是构建堆的代码:

public static void BUILD_HEAP(int[] A) {
    heapSize = A.length;
    for (int i = (int) Math.floor(A.length / 2.0); i >= 0; i--) {
        HEAPIFY(A, i);
    }
}

build heap also works and the only method that doesnot work is heapsort. 构建堆也可以工作,唯一不起作用的方法是堆排序。

 public static void HEAPSORT(int[] A) {
    BUILD_HEAP(A);
    for (int i = A.length-1; i >= 1; i--) {
        int temp = A[0];
        A[0] = A[i];
        A[i] = temp;
        heapSize = heapSize-1;
        HEAPIFY(A,0);
    }
}

this has to sort but when i try to traverse the array after the call of heapsort it does not give the sorted array. 这必须进行排序,但是当我在调用heapsort之后尝试遍历数组时,它没有给出已排序的数组。 any ideas how to fix heapsort? 任何想法如何解决堆排序?

Parent(i) return Math.floor(i/2) 父级(i)返回Math.floor(i / 2)

=> Parent(i) return Math.floor((i - 1) / 2) => Parent(i)返回Math.floor((i-1)/ 2)

Left(i) return 2i Left(i)返回2i

=> Left(i) return 2i + 1 => Left(i)返回2i + 1

Right(i) return 2i+1 向右(i)返回2i + 1

=> Right(i) return 2i + 2 => Right(i)返回2i + 2

You can work this out either by fiddling around (which is what I actually did) or considering j = i - 1. 您可以通过摆弄(这是我实际上所做的)或考虑j = i-1来解决。

If i' = 2 i and j = i - 1 so i = j + 1 如果i'= 2 i并且j = i-1则i = j + 1

j' = i' - 1 = (2i) - 1 = (2(j + 1)) - 1 = 2j + 1 j'= i'-1 =(2i)-1 =(2(j + 1))-1 = 2j + 1

if you want to start form index 1,then you can initialize the array like this: [-x,16,14,10,8,7,9,3,2,4,1] -x is the array[0],in other words,you can ignore the element which is in array[0]. 如果要启动表格索引1,则可以这样初始化数组:[-x,16,14,10,8,7,9,3,2,4,1] -x是数组[0] ,换句话说,您可以忽略array [0]中的元素。

if you want to start form index 0,then you have to modify the function LEFT(i) and RIGHT(i). 如果要开始从索引0开始,则必须修改函数LEFT(i)和RIGHT(i)。

LEFT(i) return 2*i+1; LEFT(i)返回2 * i + 1;

RIGHT(i) return 2*i+2; RIGHT(i)返回2 * i + 2;

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

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