简体   繁体   English

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

[英]sort array using heap sort

class HeapSort{
   public static void main(String args[]){
      int A[]={6,5,4,3,2,1};
      HeapSor obj=new HeapSor(6);
      obj.Heap_Sort(A);
      obj.print(A);
   }
}


class HeapSor{

    int lenOfArray;
    int HeapSize;

    HeapSor(int len){
    this.lenOfArray=len;
    this.HeapSize=len;
    }

    void Heap_Sort(int A[]){
    for(int i=0; i<lenOfArray; i++){
        BuiltHeap(A);
        HeapSize--;
        swap(A,i,lenOfArray-i);
    }
    }

    void BuiltHeap(int A[]){
    for(int i=lenOfArray/2; i>=0; i--)
        MaxHeapify(A,i);
    }

    void MaxHeapify(int A[],int i){
    int l=2*i;
    int r=2*i+1;
    int max=i;

    if(i>HeapSize)
        return;
    if(A[l]>A[r])
        max=l;
    else
        max=r;

    if(A[i]<A[max])
        swap(A,i,max);
    //max=i;
    }

    void swap(int A[],int i,int j){
    if(i==j)
        return;

    int temp=A[i];
    A[i]=A[j];
    A[j]=temp;
    }

    void print(int A[]){
    for(int i=0; i<lenOfArray; i++)
        System.out.print(A[i]+" ");

    System.out.println();
    }
}

when I compiled it gave me this error 当我编译它给我这个错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at HeapSor.MaxHeapify(HeapSort.java:41)
    at HeapSor.BuiltHeap(HeapSort.java:31)
    at HeapSor.Heap_Sort(HeapSort.java:23)
    at HeapSort.main(HeapSort.java:5)

I really tried to now what is wrong but I failed plz any one can tell me what is my wrong? 我真的尝试过现在有什么问题,但是我失败了,请问有人可以告诉我我有什么问题吗?

sorry for my bad english 对不起,我的英语不好

Two problems to start (there will be more). 有两个问题要开始(会有更多问题)。

1) Java is not C. Find the length of A using A.length not passing a separate variable. 1)Java不是C。使用A.length不传递单独的变量来查找A的长度。

2) your calculation of l and r is broken. 2)您对l和r的计算被破坏了。 You pass in 6/2 = 3 you then get 2*3 and 2*3+1 ( 6 and 7 ) for your indicies. 您传入6/2 = 3 ,然后得到2*32*3+167 )。 Neither of those is valid. 这些都不是有效的。

My guess is that your problem is here: void MaxHeapify(int A[],int i) 我的猜测是您的问题在这里: void MaxHeapify(int A[],int i)

You assign left and right child: 您分配左右孩子:

int l=2*i;  
int r=2*i+1; 

But you don't check if they are out of bounds. 但是,您不必检查它们是否超出范围。 You check for i . 你检查i

if(i>HeapSize)  
        return;

But 2*i could be out of bounds and you use it: 但是2*i可能超出范围,您可以使用它:

 if(A[l]>A[r])  
        max=l;  
    else  
        max=r;  
void max_heapify(int a[],int i,int n) {
    int mxPos = i;
    int l = i*2; // left child
    int r = i*2+1; // right child

    if( l <= n and a[l] > a[mxPos] ) mxPos = l;
    if( r <= n and a[r] > a[mxPos] ) mxPos = r;

    if( mxPos != i ) {
        swap( a[i] , a[mxPos] );
        max_heapify( a , mxPos , n );
    }
}

void build_max_heap( int a[] ,int n) {
    for(int i = n / 2 ; i >= 1 ; i-- ) max_heapify(a,i,n);
}

void heapsort(int a[],int n) {
    build_max_heap(a,n);
    for( int i = n ; i >= 2 ; i-- ) {
        swap( a[1] , a[i] );
        n--;
        max_heapify( a , 1 , n );
    }
}

int main() {
    int n , a [100] ;
    cin >> n ;
    for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
    heapsort(a,n);
    for( int i = 1 ; i <= n ; i++ ) cout << a[i] << endl;
}

This worked for me: 这为我工作:

package Heap;

public class HeapSort {

    private static int[] a;
    private static int n;
    private static int left;
    private static int right;
    private static int largest;

    public static void buildheap(int[] a) {
        n = a.length - 1;
        for (int i = n / 2; i >= 0; i--) {
            maxheap(a, i);
        }
    }

    public static void maxheap(int[] a, int i) {
        left = 2 * i;
        right = 2 * i + 1;
        if (left <= n && a[left] > a[i]) {
            largest = left;
        } else {
            largest = i;
        }

        if (right <= n && a[right] > a[largest]) {
            largest = right;
        }
        if (largest != i) {
            exchange(i, largest);
            maxheap(a, largest);
        }
    }

    public static void exchange(int i, int j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    public static void sort(int[] a0) {
        a = a0;
        buildheap(a);

        for (int i = n; i > 0; i--) {
            exchange(0, i);
            n = n - 1;
            maxheap(a, 0);1
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a1 = { 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };
        sort(a1);
        for (int i = 0; i < a1.length; i++) {
            System.out.print(a1[i] + " ");
        }

    }

}

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

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